Toolbar navigation icon not set

I tried a new toolbar component and had some problems with the navigation icon. I want to implement a custom icon for reverse navigation:

In my manifest, I set the parent for my activity:

<activity android:name=".CardsActivity" android:parentActivityName=".MainActivity"> <!-- Parent activity meta-data to support API level 7+ --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> 

I declare the toolbar as follows:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.lollitest.MainActivity" > <android.support.v7.widget.Toolbar android:id="@+id/my_awesome_toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" android:layout_marginBottom="10dp" android:background="?attr/colorPrimary" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/my_awesome_toolbar" android:text="@string/hello_world" /> </RelativeLayout> 

Then, in my work, I customize the toolbar as follows:

 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); toolbar.setNavigationIcon(R.drawable.ic_good); toolbar.setTitle("Title"); toolbar.setSubtitle("Sub"); toolbar.setLogo(R.drawable.ic_launcher); setSupportActionBar(toolbar); 

Which gives me: Toolbar with back button

The back icon is not the one I set with setNavigationIcon() ! No matter what I can give the method, the navigation icon is always a back arrow.

I tried to remove the parent association in the manifest, but the only effect (obviously) is to prevent the button from returning.

Otherwise, if I want the default back arrow icon and don't call setNavigationIcon() , I donโ€™t have the icon at all.

What is the correct way to handle the navigation icon in the toolbar (custom and default)?

NOte: I run my test on Android 4.4

+58
android material-design appcompat toolbar android-actionbar-compat
Oct 23 '14 at 9:37
source share
12 answers

You can currently use it by changing the order: (this seems to be a mistake)

 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_good); toolbar.setTitle("Title"); toolbar.setSubtitle("Sub"); toolbar.setLogo(R.drawable.ic_launcher); 
+110
Oct 23 '14 at 10:04
source share

Specific to the navigation icon, this is the correct order

 // get the actionbar as Toolbar and set it up Toolbar toolbar = (Toolbar) findViewById(R.id.signIn_toolbar); setSupportActionBar(toolbar); 

Report toolbars to provide reverse navigation. This will set the icon to the default material icon

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

Override the custom icon later, in my case the icon is back to back

 toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_36dp); 
+22
Mar 10 '15 at 14:25
source share

(Answer to user 802421)

 private void setToolbar() { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar != null) { setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_action_back); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } } 

toolbar.xml

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="@dimen/toolbar_height" android:background="?attr/colorPrimaryDark" /> 
+18
Feb 09 '15 at 5:57
source share

I have found a solution. It is really very simple:

 mDrawerToggle.setDrawerIndicatorEnabled(false); 

Hope this helps you.

+4
May 12 '15 at 5:21
source share

I had a simillar problem. After a big headache, I found that my ActionBarDrawerToggle changed the icon, also when it should not change the icon (because I did not refer to the toolbar on the switching component). So in the class NavigationDrawerFragment (which handles opening and closing) in the setUp(...) method, I set
mDrawerToggle.setHomeAsUpIndicator(R.drawable.app_icon);
and finally it worked.

+3
Feb 25 '15 at 10:34
source share

I tried to customize a toolbar like @Gabriele Mariotti, but I had a problem with the name. So I set the order

 toolbar.setTitle("Title") setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_good); 

and it works.

+3
May 10 '15 at 19:45
source share

I used a method that really is a mystery to all of the above. I also found that onOptionsItemSelected is never activated.

  mDrawerToggle.setDrawerIndicatorEnabled(false); getSupportActionBar().setHomeButtonEnabled(true); Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); if (toolbar != null) { toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } 
+3
Aug 28 '15 at 15:30
source share

You can use the invalidate() method to change the status of a toolbar anywhere. Example:

 Toolbar toolbar = (Toolbar)findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.mipmap.arrow_white); toolbar.invalidate(); // restore toolbar 
+3
Sep 09 '15 at 11:16
source share

Use setNavigationIcon to change it. Remember to create an ActionBarDrawerToggle first!

sample code works for me:

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = (DrawerLayout)findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); toolbar.setNavigationIcon(R.drawable.ic_menu); 
+3
Sep 20 '16 at 7:24
source share

Remove this line from activity if you added

  @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } 

Then set the icon

  getSupportActionBar().setHomeAsUpIndicator(icon); 
+1
Jan 23 '17 at 10:04 on
source share

Try the following:

  <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:toolbar="http://schemas.android.com/apk/res-auto" android:id="@+id/tool_drawer" android:layout_width="match_parent" android:layout_height="?actionBarSize" toolbar:navigationIcon="@drawable/ic_navigation"> </android.support.v7.widget.Toolbar> 
0
Aug 10 '16 at 10:22
source share

work for me ...

 <android.support.v7.widget.Toolbar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/toolBar" android:background="@color/colorGreen" app:title="Title" app:titleTextColor="@color/colorBlack" app:navigationIcon="@drawable/ic_action_back"/> 
0
Sep 22 '16 at 13:16
source share



All Articles