I'm in search of a way to style the ActionBar's "up" icon that is displayed when you configured it to be displayed from your Activity :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I've been looking around and found a way to add padding between the home icon and the title (Padding between ActionBar's home icon and title), but not a way to add padding between the home icon and the up indicator icon.
I dug into ActionBarView.HomeView
in the android source and it appears to inflate the up indicator via this call :
mUpView = findViewById(com.android.internal.R.id.up);
Problem is the com.android.internal.R.id.up
is internal so I can't access it, so the above linked solution for padding the home icon won't work. Ideally I'd like to override the ActionBarView.HomeView
, but it's private within the ActionBarView
.
Does anyone know if ActionBarSherlock provides a good way to override this behavior? I've looked into providing a custom home layout, but the threads I found indicated that it wouldn't be a good idea to diverge from the standard android classes.
Any ideas?
This solution will work
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(10, 0, 0, 0);
BUT, you'll be forced to put this code in a parent Activity, or in every activity you want to add the padding to the home button.
I suggest this approach:
Create a drawable layer-list with the following code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:antialias="true"
android:gravity="center"
android:src="@drawable/btn_back_actionbar_normal" />
</item>
<item>
<shape android:shape="rectangle" >
<size android:width="24dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
In your styles add:
For values-11:
<item name="android:homeAsUpIndicator">@drawable/actionbar_back_selector</item>
For values:
<item name="homeAsUpIndicator">@drawable/actionbar_back_selector</item>
This way you'll be adding a padding between the back button, and the Home button in the compat actionbar, and you'll have to add this code only once, and will be available in the whole app!
As of API 15 the positioning of these elements are hard-coded into the layout resources. The only alternative you have would be to use a custom navigation layout to create your own and hide the built-in home navigation.
Actually this answer is correct. Just set the left padding of the home button and it will add padding between the home button and the up arrow
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(10, 0, 0, 0);
Maybe it works if you add the padding to the graphic (in Photoshop, for example) and set this graphic on the theme with the attribute
<item name="android:homeAsUpIndicator">@drawable/up_with_padding</item>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With