Thursday, April 25, 2013

Solution: Actionbar Space or Padding between homeasupindicator and app icon

Actionbar support starts from android version 3.0+

If you want to apply style for actionbar then add values-v11 is the values of the API version 11, and values-v14 is the values of the API version 14 in the res folder/ when your minSdkVersion is less than 11.
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" />

Using Themes, getting space between homeAsUpIndicator and App Icon is not working but it is possible through programatically.

android:homeAsUpIndicator : Specifies a drawable to use for the 'home as up' indicator.

programatically:
setDisplayHomeAsUpEnabled(true) shows the "<" on the action bar.

Style:
android:homeAsUpIndicator allows to change the icon

<item name="homeAsUpIndicator">@drawable/custom_home</item>



Solution:
Access the homeAsUpIndicator from the app by calling the android.R.id.home.
Change padding so that we will get space between homeAsUpIndicator and App Icon.
((ImageView) android.R.id.home).setPadding(7,0,7,0)




4 comments :

  1. Hi,

    Not working as you said.

    Would you please post full snippet of code.

    I extended my activity with SherlockFragmentActivity.

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#08A3F5")));
    actionBar.setLogo(R.drawable.menu_dark);

    Now I want to set padding at left side of setDisplayHomeAsUpEnabled. I do not want to show the < icon so I have

    style name="AppBaseTheme" parent="Theme.Sherlock.Light"

    name="homeAsUpIndicator" --->@null
    name="android:homeAsUpIndicator" -->@null


    Thanks.

    ReplyDelete
    Replies
    1. The code is shown not for sherlock library.
      One question
      In your code, you haven't set padding. then how come, it will work?

      Use the below code, set padding for the home icon in onCreate of your activity.
      ((ImageView) android.R.id.home).setPadding(7,0,7,0)
      In styles or themes, we can't give padding for action bar.

      Delete
  2. ImageView view = (ImageView)findViewById(android.R.id.home);
    view.setPadding(7,0,0,0);
    this works fine :-)

    ReplyDelete
  3. nice it worked for me.
    ImageView view = (ImageView)findViewById(android.R.id.home);
    view.setPadding(7,0,0,0);
    this works fine :-)

    ReplyDelete