Search code examples
android-actionbarandroidandroid-stylesandroid-homebutton

Change homeAsUpIndicator dynamically


I build an app with four tabs and, as a consequence, four fragments. In the first fragment, I have a sliding menu and I throw with a custom icon in the actionbar. The problem is that in the others fragments, I want to stablish other custom icon to return to the first fragment but I don't know what to do...

This is my styles.xml with my custom icon for the sliding menu:

<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:homeAsUpIndicator">@drawable/menu32</item>
    </style>

The java code works well but I don't know how to change the icon dynamically.

This is what I have:


Solution

  • You can use the following in your fragments:

    Actionbar ab = getActivity().getActionBar();
    ab.setIcon(icon);
    

    Where icon is a Drawable or a resource id, when the up indicator is a part of your icon.
    You can also use setHomeAsUpIndicator if you use API level > 11:

    getActivity().getActionBar().setHomeAsUpIndicator(resId);
    

    If your API level is lower, then I suggest you make use of ActionbarSherlock.