I want to remove the padding around the icon on the left in the standard android 4.0+ action bar. I\'m setting the icon with:
getActionBar().setIcon(getResou
Digging into AOSP sources, it seems the code involved is in com.android.internal.widget.ActionBarView.java. In particular the relevant part is the onLayout()
method of the inner class ActionBarView$HomeView
, partially reported below (lines 1433-1478):
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
...
final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
final int iconHeight = mIconView.getMeasuredHeight();
final int iconWidth = mIconView.getMeasuredWidth();
final int hCenter = (r - l) / 2;
final int iconTop = Math.max(iconLp.topMargin, vCenter - iconHeight / 2);
final int iconBottom = iconTop + iconHeight;
final int iconLeft;
final int iconRight;
int marginStart = iconLp.getMarginStart();
final int delta = Math.max(marginStart, hCenter - iconWidth / 2);
if (isLayoutRtl) {
iconRight = width - upOffset - delta;
iconLeft = iconRight - iconWidth;
} else {
iconLeft = upOffset + delta;
iconRight = iconLeft + iconWidth;
}
mIconView.layout(iconLeft, iconTop, iconRight, iconBottom);
}
the same widget use this layout, defined in res/layout/action_bar_home.xml:
According to sources the icon is shown in the Imageview
with id=android.R.id.home
. The onLayout()
method reported above takes account of the ImageView
margins defined in the layout, which can't be set via theme/style override because they use the value @android:dimen/action_bar_icon_vertical_padding
.
All you can do is to get rid of those values at runtime, and set them according your needs:
simply retrieve the ImageView
and set its top and bottom margins to 0
. Something like this:
ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams( iconLp );
EDIT: I've just realized I didn't cover how to get rid of left padding. Solution below.
Left padding on actionbar is affected by the Navigating Up behavior of actionbar icon. When that is disabled (via ActionBar.setDisplayHomeAsUpEnabled(false)
) the left/up indicator is gone, but a left padding is used as well. A simple solution:
ActionBar.setDisplayHomeAsUpEnabled(true)
in order to consider the indicator view in the layout processres/values-v14/styles.xml
to null
eg: