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(getResources().getDrawable(R.drawable.ic_action_myapp));
And I would like the icon to fill vertically the space, touching both top and bottom, similar to what soundcloud app does:
use custom layout for ActionBar
actionbar_custom_view_home.xml
you can find homeview in actionbarview define like this:
but you cannot get upView by
findViewById(android.R.id.up)
.so you can get homeView and get its parent view ,set upview width 0
I found an other resolution (reference appcompat-v7 ) that change the toolbarStyle ,following code:
Enhanced parrzhang reply in remove padding around action bar left icon on Android 4.0+
To set the height of ActionBar you can create new Theme like this one:
and set this Theme to your Activity:
android:theme="@style/Theme.BarSize"
Now, set the height of the icons to
"match_parent"
.That would remove the top and bottom padding.
Now, the arrow at the left is inbuilt into the framework, so you have two options for a workaround:
Use ActionBarSherlock. It uses it's own drwables and resources, so you can modify the arrow icon to an emty png, so that your up icon would move to extreme left.
The up/back icon arises from:
So, instead of using this option for up button, you can make another actionbar option, which has an intent for the previous activity, and then place that icon on your action bar.
It will be a bigger workaround though.
Hope that helps.. :)
Digging into AOSP sources, it seems the code involved is in
com.android.internal.widget.ActionBarView.java
. In particular the relevant part is theonLayout()
method of the inner classActionBarView$HomeView
, partially reported below (lines 1433-1478):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
. TheonLayout()
method reported above takes account of theImageView
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 to0
. Something like this: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
tonull
eg: