List of usage examples for android.content.res Resources getDisplayMetrics
public DisplayMetrics getDisplayMetrics()
From source file:org.de.jmg.learn.MainActivity.java
private void _SetShowAsAction(final MenuItem m) { final View tb = this.findViewById(R.id.action_bar); int SizeOther = 0; int width;//from w w w . j a v a2 s .com ActionMenu = null; if (tb != null) { width = tb.getWidth(); Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); double height = metrics.heightPixels; int viewTop = this.findViewById(Window.ID_ANDROID_CONTENT).getTop(); double scale = (height - viewTop) / (double) 950; if (scale < .5f) { isSmallDevice = true; } double ActionBarHeight = tb.getHeight(); if (isSmallDevice && ActionBarHeight / height > .15f) { try { FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) tb.getLayoutParams(); layoutParams.height = (int) (height * .15f); tb.setMinimumHeight((int) (height * .15f)); tb.setLayoutParams(layoutParams); tb.requestLayout(); } catch (Exception ex) { Log.e("SetToolbarHeight", ex.getMessage(), ex); } } if (width > 0) { ViewGroup g = (ViewGroup) tb; for (int i = 0; i < g.getChildCount(); i++) { View v = g.getChildAt(i); if ((v instanceof android.support.v7.widget.ActionMenuView)) { SizeOther += v.getWidth(); ActionMenu = (android.support.v7.widget.ActionMenuView) v; } } if (SizeOther < width * .7) _blnReverse = true; if ((_blnReverse || SizeOther > width * .7) && ActionMenu != null) { if (_blnReverse) { //if (!_hasBeenDownsized || _hasBeenDownsized) //{ MenuBuilder mm = (MenuBuilder) ActionMenu.getMenu(); int Actions = mm.getActionItems().size(); try { MenuItem mmm = ActionMenu.getMenu().getItem(Actions + _invisibleCount); if (mmm.isVisible()) { MenuItemCompat.setShowAsAction(mmm, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); } else { _invisibleCount += 1; _SetShowAsAction(mmm); } } catch (IndexOutOfBoundsException ex) { return; } //} } else { MenuItemCompat.setShowAsAction(m, MenuItemCompat.SHOW_AS_ACTION_NEVER); _hasBeenDownsized = true; } ActionMenu.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (ActionMenu != null) { lib.removeLayoutListener(ActionMenu.getViewTreeObserver(), this); int SizeNew = ActionMenu.getWidth(); Log.v("Test", "" + SizeNew); MenuBuilder mm = (MenuBuilder) ActionMenu.getMenu(); int count = mm.getActionItems().size(); if (count >= 1 && !(_blnReverse && SizeNew > tb.getWidth() * .7)) { MenuItem m = mm.getActionItems().get(count - 1); _SetShowAsAction(m); } } } }); } } } }
From source file:org.de.jmg.learn.MainActivity.java
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. try {//from ww w. ja v a 2 s .co m getMenuInflater().inflate(R.menu.main, menu); //findViewById(R.menu.main).setBackgroundColor(Color.BLACK); //.setBackgroundColor(Color.BLACK); //resize(); OptionsMenu = menu; if (OptionsMenu != null) { mnuAddNew = menu.findItem(R.id.mnuAddWord); mnuUploadToQuizlet = menu.findItem(R.id.mnuUploadToQuizlet); Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); int height = metrics.heightPixels; int viewTop = this.findViewById(Window.ID_ANDROID_CONTENT).getTop(); height = height - viewTop; double scale = (double) height / (double) 950; if (scale < .5f) { isSmallDevice = true; } if (Build.VERSION.SDK_INT < 11) { menu.findItem(R.id.mnuOpenQuizlet).setVisible(false); menu.findItem(R.id.mnuUploadToQuizlet).setVisible(false); } /* if (isSmallDevice) { MenuItemCompat.setShowAsAction(menu.findItem(R.id.mnuSaveAs), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); } */ MenuItem mnuQuizlet = menu.findItem(R.id.mnuLoginQuizlet); mnuQuizlet.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { //throw new RuntimeException("Test"); return false; } }); if (BuildConfig.DEBUG) { mnuQuizlet.setVisible(true); } else { mnuQuizlet.setVisible(false); } MenuItem mnuAskReverse = OptionsMenu.findItem(R.id.mnuAskReverse); mnuAskReverse.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { item.setChecked(!item.isChecked()); vok.reverse = item.isChecked(); setMnuReverse(); return true; } }); setMnuReverse(); /* if (isSmallDevice) { MenuItem m = menu.findItem(R.id.mnuHome); MenuItemCompat.setShowAsAction(m,MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); m = menu.findItem(R.id.mnuFileOpen); MenuItemCompat.setShowAsAction(m,MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); m = menu.findItem(R.id.mnuSaveAs); MenuItemCompat.setShowAsAction(m,MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); m = menu.findItem(R.id.mnuAddWord); MenuItemCompat.setShowAsAction(m,MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); //lib.ShowToast(this,"Menus set to IF_ROOM!"); } */ mainView.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Ensure you call it only once : lib.removeLayoutListener(mainView.getViewTreeObserver(), this); //MainActivity.this.SetShowAsAction(mnuUploadToQuizlet); } }); return true; } throw new RuntimeException("menu is null!"); } catch (Exception ex) { lib.ShowException(this, ex); } return false; }
From source file:com.google.android.apps.gutenberg.ScannerActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scanner); // Resources/*from ww w . ja v a 2 s.c o m*/ final Resources resources = getResources(); mColorTabSelected = resources.getColor(R.color.tab_selected); mColorTabUnselected = resources.getColor(R.color.tab_unselected); mColorTabIndicator = resources.getColor(R.color.primary_dark); mToolbarElevation = resources.getDimensionPixelSize(R.dimen.toolbar_elevation); GutenbergApplication app = GutenbergApplication.from(this); mImageLoader = new ImageLoader(app.getRequestQueue(), app.getBitmapCache()); // Set up the Toolbar Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Set up the Fragments if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().replace(R.id.scanner, ScannerFragment.newInstance()) .replace(R.id.event_selector, EventSelectionFragment.newInstance()).commit(); } // Set up the ViewPager mPanelLayout = (RecyclerViewSlidingUpPanelLayout) findViewById(R.id.layout); mTabLayout = (TabLayout) findViewById(R.id.tabs); mTabLayout.getViewTreeObserver().addOnGlobalLayoutListener(mTabLayoutListener); mViewPager = (DrawerViewPager) findViewById(R.id.pager); mAdapter = new ScannerPagerAdapter(getSupportFragmentManager(), this); mViewPager.setAdapter(mAdapter); mTabLayout.addTabsFromPagerAdapter(mAdapter); int tabCount = mAdapter.getCount(); mTabs = new AppCompatTextView[tabCount]; LayoutInflater inflater = LayoutInflater.from(this); for (int i = 0; i < tabCount; i++) { mTabs[i] = (AppCompatTextView) inflater.inflate(R.layout.tab, mTabLayout, false); mTabs[i].setText(mAdapter.getPageTitle(i)); mTabLayout.getTabAt(i).setCustomView(mTabs[i]); } mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition(), true); mTabs[tab.getPosition()].setTextColor(mColorTabSelected); } @Override public void onTabUnselected(TabLayout.Tab tab) { mTabs[tab.getPosition()].setTextColor(mColorTabUnselected); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); mPanelLayout.setPanelSlideListener(mPanelSlideListener); mViewPager.setPageMargin((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, VIEW_PAGER_PAGE_MARGIN, resources.getDisplayMetrics())); mViewPager.setPageMarginDrawable(resources.getDrawable(R.drawable.page_margin)); final ViewPager.OnPageChangeListener onPageChangeListener = mTabLayout.createOnPageChangeListener(); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } @Override public void onPageSelected(int position) { onPageChangeListener.onPageSelected(position); mPanelLayout.setRecyclerView(mAdapter.getItem(position).getRecyclerView()); } }); mAdapter.getItem(mViewPager.getCurrentItem()) .setOnRecyclerViewReadyListener(new RecyclerViewFragment.OnRecyclerViewReadyListener() { @Override public void onRecyclerViewReady(RecyclerView rv) { mPanelLayout.setRecyclerView(rv); } }); mViewPager.setOnClickListenerWhenClosed(new View.OnClickListener() { @Override public void onClick(View v) { mPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED); } }); }
From source file:android.support.design.widget.CoordinatorLayout.java
public CoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); ThemeUtils.checkAppCompatTheme(context); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CoordinatorLayout, defStyleAttr, R.style.Widget_Design_CoordinatorLayout); final int keylineArrayRes = a.getResourceId(R.styleable.CoordinatorLayout_keylines, 0); if (keylineArrayRes != 0) { final Resources res = context.getResources(); mKeylines = res.getIntArray(keylineArrayRes); final float density = res.getDisplayMetrics().density; final int count = mKeylines.length; for (int i = 0; i < count; i++) { mKeylines[i] *= density;/*from w w w . j a v a 2 s . co m*/ } } mStatusBarBackground = a.getDrawable(R.styleable.CoordinatorLayout_statusBarBackground); a.recycle(); setupForInsets(); super.setOnHierarchyChangeListener(new HierarchyChangeListener()); }
From source file:de.vanita5.twittnuker.util.Utils.java
public static void addIntentToMenu(final Context context, final Menu menu, final Intent queryIntent, final int groupId) { if (context == null || menu == null || queryIntent == null) return;//from ww w . j ava 2 s .co m final PackageManager pm = context.getPackageManager(); final Resources res = context.getResources(); final float density = res.getDisplayMetrics().density; final int padding = Math.round(density * 4); final List<ResolveInfo> activities = pm.queryIntentActivities(queryIntent, 0); for (final ResolveInfo info : activities) { final Intent intent = new Intent(queryIntent); final Drawable icon = info.loadIcon(pm); intent.setClassName(info.activityInfo.packageName, info.activityInfo.name); final MenuItem item = menu.add(groupId, Menu.NONE, Menu.NONE, info.loadLabel(pm)); item.setIntent(intent); final int iw = icon.getIntrinsicWidth(), ih = icon.getIntrinsicHeight(); if (iw > 0 && ih > 0) { final Drawable iconWithPadding = new PaddingDrawable(icon, padding); iconWithPadding.setBounds(0, 0, iw, ih); item.setIcon(iconWithPadding); } else { item.setIcon(icon); } } }
From source file:com.example.ray.firstapp.bottombar.BottomBar.java
private static void navBarMagic(Activity activity, final BottomBar bottomBar) { Resources res = activity.getResources(); int softMenuIdentifier = res.getIdentifier("config_showNavigationBar", "bool", "android"); int navBarIdentifier = res.getIdentifier("navigation_bar_height", "dimen", "android"); int navBarHeight = 0; if (navBarIdentifier > 0) { navBarHeight = res.getDimensionPixelSize(navBarIdentifier); }//from w w w .j av a 2 s. c o m if (!bottomBar.drawBehindNavBar() || navBarHeight == 0 || (!(softMenuIdentifier > 0 && res.getBoolean(softMenuIdentifier)))) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && ViewConfiguration.get(activity).hasPermanentMenuKey()) { return; } /** * Copy-paste coding made possible by: * http://stackoverflow.com/a/14871974/940036 */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Display d = activity.getWindowManager().getDefaultDisplay(); DisplayMetrics realDisplayMetrics = new DisplayMetrics(); d.getRealMetrics(realDisplayMetrics); int realHeight = realDisplayMetrics.heightPixels; int realWidth = realDisplayMetrics.widthPixels; DisplayMetrics displayMetrics = new DisplayMetrics(); d.getMetrics(displayMetrics); int displayHeight = displayMetrics.heightPixels; int displayWidth = displayMetrics.widthPixels; boolean hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; if (!hasSoftwareKeys) { return; } } /** * End of delicious copy-paste code */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { activity.getWindow().getAttributes().flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if (bottomBar.useTopOffset()) { int offset; int statusBarResource = res.getIdentifier("status_bar_height", "dimen", "android"); if (statusBarResource > 0) { offset = res.getDimensionPixelSize(statusBarResource); } else { offset = MiscUtils.dpToPixel(activity, 25); } if (!bottomBar.useOnlyStatusbarOffset()) { TypedValue tv = new TypedValue(); if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { offset += TypedValue.complexToDimensionPixelSize(tv.data, res.getDisplayMetrics()); } else { offset += MiscUtils.dpToPixel(activity, 56); } } bottomBar.getUserContainer().setPadding(0, offset, 0, 0); } final View outerContainer = bottomBar.getOuterContainer(); final int navBarHeightCopy = navBarHeight; bottomBar.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { bottomBar.shyHeightAlreadyCalculated(); int newHeight = outerContainer.getHeight() + navBarHeightCopy; outerContainer.getLayoutParams().height = newHeight; if (bottomBar.isShy()) { int defaultOffset = bottomBar.useExtraOffset() ? navBarHeightCopy : 0; bottomBar.setTranslationY(defaultOffset); ((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams()) .setBehavior(new BottomNavigationBehavior(newHeight, defaultOffset)); } ViewTreeObserver obs = outerContainer.getViewTreeObserver(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { obs.removeOnGlobalLayoutListener(this); } else { obs.removeGlobalOnLayoutListener(this); } } }); } }
From source file:com.harlan.jxust.ui.view.bottombar.BottomBar.java
private static void navBarMagic(Activity activity, final BottomBar bottomBar) { Resources res = activity.getResources(); int softMenuIdentifier = res.getIdentifier("config_showNavigationBar", "bool", "android"); int navBarIdentifier = res.getIdentifier("navigation_bar_height", "dimen", "android"); int navBarHeight = 0; if (navBarIdentifier > 0) { navBarHeight = res.getDimensionPixelSize(navBarIdentifier); }/* w w w. j a v a 2s. co m*/ if (!bottomBar.drawBehindNavBar() || navBarHeight == 0 || (!(softMenuIdentifier > 0 && res.getBoolean(softMenuIdentifier)))) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && ViewConfiguration.get(activity).hasPermanentMenuKey()) { return; } /** * Copy-paste coding made possible by: * http://stackoverflow.com/a/14871974/940036 */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Display d = activity.getWindowManager().getDefaultDisplay(); DisplayMetrics realDisplayMetrics = new DisplayMetrics(); d.getRealMetrics(realDisplayMetrics); int realHeight = realDisplayMetrics.heightPixels; int realWidth = realDisplayMetrics.widthPixels; DisplayMetrics displayMetrics = new DisplayMetrics(); d.getMetrics(displayMetrics); int displayHeight = displayMetrics.heightPixels; int displayWidth = displayMetrics.widthPixels; boolean hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; if (!hasSoftwareKeys) { return; } } /** * End of delicious copy-paste code */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { activity.getWindow().getAttributes().flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if (bottomBar.useTopOffset()) { int offset; int statusBarResource = res.getIdentifier("status_bar_height", "dimen", "android"); if (statusBarResource > 0) { offset = res.getDimensionPixelSize(statusBarResource); } else { offset = MiscUtils.dpToPixel(activity, 25); } if (!bottomBar.useOnlyStatusbarOffset()) { TypedValue tv = new TypedValue(); if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { offset += TypedValue.complexToDimensionPixelSize(tv.data, res.getDisplayMetrics()); } else { offset += MiscUtils.dpToPixel(activity, 56); } } bottomBar.getUserContainer().setPadding(0, offset, 0, 0); } final View outerContainer = bottomBar.getOuterContainer(); final int navBarHeightCopy = navBarHeight; bottomBar.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { bottomBar.shyHeightAlreadyCalculated(); int newHeight = outerContainer.getHeight() + navBarHeightCopy; outerContainer.getLayoutParams().height = newHeight; if (bottomBar.isShy()) { int defaultOffset = bottomBar.useExtraOffset() ? navBarHeightCopy : 0; bottomBar.setTranslationY(defaultOffset); ((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams()) .setBehavior(new BottomNavigationBehavior(newHeight, defaultOffset, bottomBar.isShy(), bottomBar.mIsTabletMode)); } ViewTreeObserver obs = outerContainer.getViewTreeObserver(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { obs.removeOnGlobalLayoutListener(this); } else { obs.removeGlobalOnLayoutListener(this); } } }); } }
From source file:ticwear.design.widget.CoordinatorLayout.java
public CoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); if (!isInEditMode()) { ThemeUtils.checkDesignTheme(context); }/*from ww w .j av a2s.c o m*/ final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CoordinatorLayout, defStyleAttr, defStyleRes); final int keylineArrayRes = a.getResourceId(R.styleable.CoordinatorLayout_tic_keylines, 0); if (keylineArrayRes != 0) { final Resources res = context.getResources(); mKeylines = res.getIntArray(keylineArrayRes); final float density = res.getDisplayMetrics().density; final int count = mKeylines.length; for (int i = 0; i < count; i++) { mKeylines[i] *= density; } } mStatusBarBackground = a.getDrawable(R.styleable.CoordinatorLayout_tic_statusBarBackground); int effect = a.getInt(R.styleable.CoordinatorLayout_tic_overScrollEffect, OverScrollEffect.BOUNCE.ordinal()); mOverScrollEffect = OverScrollEffect.values()[effect]; a.recycle(); super.setOnHierarchyChangeListener(new HierarchyChangeListener()); mScrollBarHelper = new ScrollBarHelper(context, attrs, defStyleAttr); mViewScrollingStatusAccessor = new ViewScrollingStatusAccessor(); if (mEdgeGlowTop == null) { mEdgeGlowTop = new ClassicEdgeEffect(context); mEdgeGlowBottom = new ClassicEdgeEffect(context); } ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { if (mLastInsets == null) { setupForWindowInsets(); } if (isShown()) { setWindowInsets(insets); return insets.consumeSystemWindowInsets(); } else { return insets; } } }); if (isInEditMode()) { mOverScrollOffsetFactor = 0.5f; mOverScrollOffsetLimit = Integer.MAX_VALUE; } else { TypedValue typedValue = new TypedValue(); getResources().getValue(R.integer.design_factor_over_scroll_bounce, typedValue, true); mOverScrollOffsetFactor = typedValue.getFloat(); mOverScrollOffsetLimit = getResources().getDimensionPixelOffset(R.dimen.design_over_scroll_limit); } setWillNotDraw(false); }
From source file:org.getlantern.firetweet.util.Utils.java
public static void addIntentToMenuForExtension(final Context context, final Menu menu, final int groupId, final String action, final String parelableKey, final String parelableJSONKey, final FiretweetParcelable parcelable) { if (context == null || menu == null || action == null || parelableKey == null || parcelable == null) return;// ww w.j av a2s . c om final PackageManager pm = context.getPackageManager(); final Resources res = context.getResources(); final float density = res.getDisplayMetrics().density; final int padding = Math.round(density * 4); final Intent queryIntent = new Intent(action); queryIntent.setExtrasClassLoader(context.getClassLoader()); final List<ResolveInfo> activities = pm.queryIntentActivities(queryIntent, PackageManager.GET_META_DATA); for (final ResolveInfo info : activities) { final Intent intent = new Intent(queryIntent); if (isExtensionUseJSON(info)) { intent.putExtra(parelableJSONKey, JSONSerializer.toJSONObjectString(parcelable)); } else { intent.putExtra(parelableKey, parcelable); } intent.setClassName(info.activityInfo.packageName, info.activityInfo.name); final MenuItem item = menu.add(groupId, Menu.NONE, Menu.NONE, info.loadLabel(pm)); item.setIntent(intent); final Drawable metaDataDrawable = getMetadataDrawable(pm, info.activityInfo, METADATA_KEY_EXTENSION_ICON); final int actionIconColor = ThemeUtils.getThemeForegroundColor(context); if (metaDataDrawable != null) { metaDataDrawable.mutate(); metaDataDrawable.setColorFilter(actionIconColor, Mode.SRC_ATOP); item.setIcon(metaDataDrawable); } else { final Drawable icon = info.loadIcon(pm); final int iw = icon.getIntrinsicWidth(), ih = icon.getIntrinsicHeight(); if (iw > 0 && ih > 0) { final Drawable iconWithPadding = new PaddingDrawable(icon, padding); iconWithPadding.setBounds(0, 0, iw, ih); item.setIcon(iconWithPadding); } else { item.setIcon(icon); } } } }