Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding the actionbar "up" indicator image

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?

like image 237
dgknipp Avatar asked May 22 '12 17:05

dgknipp


4 Answers

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:

  1. 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>
    
  2. 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!

like image 184
David_E Avatar answered Nov 09 '22 16:11

David_E


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.

like image 44
Jake Wharton Avatar answered Nov 09 '22 15:11

Jake Wharton


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);
like image 7
starkej2 Avatar answered Nov 09 '22 16:11

starkej2


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>
like image 2
miguel.rodelas Avatar answered Nov 09 '22 14:11

miguel.rodelas