Search code examples
androidactionbarsherlockandroid-actionbar

Change the actionbar homeAsUpIndicator Programmatically


I used the following hack to change the homeAsupIndicator programmatically.

int upId = Resources.getSystem().getIdentifier("up", "id", "android");
if (upId > 0) {
    ImageView up = (ImageView) findViewById(upId);
up.setImageResource(R.drawable.ic_action_bar_menu);
up.setPadding(0, 0, 20, 0);
}

But this is not working on most new phones (HTC One, Galaxy S3, etc). Is there a way that can be changed uniformly across devices. I need it to be changed only on home screen. Other screens would have the default one. So cannot use the styles.xml


Solution

  • This is what i did to acheive the behavior. I inherited the base theme and created a new theme to use it as a theme for the specific activity.

    <style name="CustomActivityTheme" parent="AppTheme">
        <item name="android:homeAsUpIndicator">@drawable/custom_home_as_up_icon</item>
    </style>
    

    and in the android manifest i made the activity theme as the above.

    <activity
            android:name="com.example.CustomActivity"
            android:theme="@style/CustomActivityTheme" >
    </activity>
    

    works great. Will update again when i check on all devices I have. Thanks @faylon for pointing in the right direction