dasar
dasar

Reputation: 5361

How to change HomeAsUp indicator in new AppCompat Toolbar?

I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.

styles (same for other API's level):

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- API 14 theme customizations can go here. -->
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:homeAsUpIndicator">@drawable/home</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>
        <item name="homeAsUpIndicator">@drawable/home</item>
</style>

Toolbar in my fragment layout:

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:homeAsUpIndicator="@drawable/home"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/AppTheme" />

In Fragment:

toolbar = (Toolbar) rootView.findViewById(R.id.my_awesome_toolbar);

        activity.setSupportActionBar(toolbar);

        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

Upvotes: 28

Views: 48955

Answers (6)

Victor Marlz Ojo
Victor Marlz Ojo

Reputation: 61

i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.

  @Override
   protected void onCreate(Bundle savedInstanceState) {

   .....
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_action_back);

Post the following code in onOptionsItemSelected(MenuItem item)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   ......
    if (id == android.R.id.home) {
        startActivity(new Intent(this, MainActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps

Upvotes: 4

Dan K
Dan K

Reputation: 46

Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

(as observed on Android 4.0.4, 4.3, and 4.4.2 phones)

Upvotes: 2

Ioane Sharvadze
Ioane Sharvadze

Reputation: 2158

Just add this line to your styles.xml

        <item name="navigationIcon">@drawable/ic_back_arrow</item>

Upvotes: 19

Nikolay Nikiforchuk
Nikolay Nikiforchuk

Reputation: 2048

If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:

//set home as up indicator
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);

//remove home as up indicator
mDrawerToggle.setHomeAsUpIndicator(null);

to show homeAsUpIndicator indicator instead of home indicator do following:

mDrawerToggle.setDrawerIndicatorEnabled(false);

Docs:

ActionBarDrawerToggle#setHomeAsUpIndicator ActionBarDrawerToggle#setDrawerIndicatorEnabled

Upvotes: 9

RockerFlower
RockerFlower

Reputation: 727

I tried setHomeAsUpIndicator(int resId) ,it works.

private void initToolbar ( Toolbar mToolbar ) {

    mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
    setSupportActionBar ( mToolbar );

    ActionBar actionbar = getSupportActionBar ();
    actionbar.setDisplayHomeAsUpEnabled ( true );
    actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
}

But mToolbar.setNavigationIcon(R.drawable.ic_action_back) doesn't work :(

Upvotes: 19

dasar
dasar

Reputation: 5361

To change icon just call at runtime:

toolbar.setNavigationIcon(R.drawable.home);

Trick with styles/themes not working.

How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21

Upvotes: 33

Related Questions