16

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?

Community
  • 1
  • 1
dgknipp
  • 163
  • 1
  • 1
  • 5
  • Fom me work that:
    http://stackoverflow.com/questions/26809133/how-to-change-toolbar-navigation-icon-and-options-menu-margin
    – Mnio Jun 03 '16 at 09:35

8 Answers8

22

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!

David_E
  • 7,130
  • 4
  • 26
  • 29
9

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.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • That's the conclusion I came to as well. I'm assuming Google is trying to enforce certain design standards. In the end I dropped back down to the default holo light home as up indicator and it looks pretty decent compared to the custom indicator I had configured. – dgknipp Jun 15 '12 at 16:17
  • This has now changed with the introduction of Toolbar in API 21 – Vinay W Mar 09 '15 at 11:53
7

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);
Community
  • 1
  • 1
starkej2
  • 11,391
  • 6
  • 35
  • 41
2

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>
MRD
  • 7,146
  • 1
  • 21
  • 21
  • 1
    I believe I tried that, I'd have to check again, but I'm pretty sure Google is eliminating any padding you might add in to keep the UI consistent. – dgknipp Jun 15 '12 at 16:18
1

In my cellphone, IT'S work. The solution is in below:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <bitmap
        android:src="@drawable/frame_menu"
        android:antialias="true"
        android:gravity="center" 
        />
</item>
<item >
    <shape android:shape="rectangle">
        <size android:width="24dp"/>
        <solid android:color="@android:color/transparent"></solid>
        <padding
            android:right="10dp" 
            />
    </shape>

</item>

The most important sentence is "android:right=10dp"

tain198127
  • 21
  • 4
0

I would just add to @blacksh33p answer how to get the logo ImageView while using ActionBarSherlock to support older phones.

ImageView view = (ImageView)findViewById(android.R.id.home);
if (view == null)
    view = (ImageView)findViewById(R.id.abs__home); // not sure that the name is exactly this - I tested it while developing an app on monodroid
Display Name
  • 8,022
  • 3
  • 31
  • 66
0

Simply Add the drawable Layout Call "actionbar_back_selector" And put this Code

And in Java File (Which layout you need home up button that corresponding java file) Put this code ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); actionBar.setHomeAsUpIndicator(R.drawable.actionbar_back_selector);

0

you may define a style for "HomeAsUpIndicator" in you theme like this:

<item name="toolbarNavigationButtonStyle">@style/MyToolbarNavigationButtonStyle</item>

in which, the toobarNavigationButtonStyle is used to define all styles for that indicator, you may change it's width and paddingEnd to enlarge gap between it and the Title text.

landerlyoung
  • 1,693
  • 17
  • 14