Appcompatv7 - v21 Navigation box not showing a hamburger icon

I am implementing a lollipop navigation box with the latest appcompat support library, but the problem is that the hamburger icon is never displayed. Only the reverse icon is displayed.

This is my activity code.

import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.View; public class Home extends ActionBarActivity { private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); initViews(); } private void initViews(){ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); setSupportActionBar(toolbar); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar , R.string.drawer_open, R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); //getActionBar().setTitle(mTitle); //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); //getActionBar().setTitle(mDrawerTitle); //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } } 

This is my stylesheet.

  <resources> <!-- Application theme. --> <style name="Theme.Test" parent="@style/Theme.AppCompat.Light"> <!-- customize the color palette --> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">@color/accent</item> <item name="windowActionBar">false</item> <item name="drawerArrowStyle">@style/Theme.Test.DrawerArrowStyle</item> </style> <style name="Theme.Test.DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style> 

Layout file

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout> 

Navigation Box Showing Return Button

Navigation Box Showing Return Button

In both cases, only the back arrow is shown, I read a lot of messages, but nothing has changed. Any help would be appreciated.

+85
android android-layout android-support-library android-navigation
Nov 05 '14 at
source share
13 answers

You need to call

 mDrawerToggle.syncState(); 
+124
Nov 05 '14 at 10:50
source share

Make sure you import the correct drawer switch.

When I imported the v4 version, I had an arrow (below).

 import android.support.v4.app.ActionBarDrawerToggle; 

Changing this setting (below, v7) fixed my problem.

 import android.support.v7.app.ActionBarDrawerToggle; 
+19
Jul 20 '15 at 2:04
source share

When using an ActionBarDrawerToggle, you must call it during onPostCreate () and onConfigurationChanged ()

  @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); } 
+12
Jan 25 '15 at 12:24
source share

Make sure you call

mDrawerToggle.syncState();

AFTER a call

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); 
+10
Jan 15 '16 at 16:02
source share

Since my NavigationDrawer expanded the fragment, not the Activity, I could not override postCreate. The following shows what I did.

  ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); // this sets the button to the back icon actionBar.setHomeButtonEnabled(true); // makes it clickable actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);// set your own icon 

Hope this helps!

+7
Aug 31 '15 at 13:02
source share

Remember to override the onOptionsItemSelected method and check if ctionBarDrawerToggle has been pressed, in this case return true, otherwise the action will complete.

 @Override public boolean onOptionsItemSelected(MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } 
+6
May 21 '15 at 10:39
source share

You can simply use this:

 // Defer code dependent on restoration of previous instance state. mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer); } }); 
+4
Sep 04 '15 at 0:18
source share

mDrawerToggle.syncState() did not work for me, but in the end I got it to work with:

 getSupportActionBar().setHomeAsUpIndicator(R.drawable.hamburger_icon); 

However, I did not use the toolbar.

+3
Jul 08 '15 at 17:02
source share

When including an ActionBarDrawerToggle, be sure to use the post method:

 mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); 
+2
Apr 04 '15 at 18:32
source share

you can call syncState () from your Activity onPostCreate to synchronize the indicator with the state of the associated DrawerLayout after the onRestoreInstanceState has happened.

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

Also, ActionBarDrawerToggle can be used directly as DrawerLayout.DrawerListener, or if you already provide your own listener, call each of the listener methods from your own.

 private ActionBarDrawerToggle mDrawerToggle; private DrawerLayout mDrawerLayout; . . . . mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); 
+1
Jun 25 '15 at 4:42
source share

This works for me. I expanded AppCompatActivity instead of ActionBarActivity.

 mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,null, R.string.drawer_opened, R.string.drawer_closed) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if( getSupportActionBar()!= null) getSupportActionBar().setTitle(R.string.drawer_opened); mActionBarDrawerToggle.syncState(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if(getSupportActionBar() != null) getSupportActionBar().setTitle(R.string.drawer_closed); mActionBarDrawerToggle.syncState(); } }; 
0
Mar 10 '16 at 1:05
source share

The navigation shortcut did not show when you click the action bar menu. This fixed it for me.

  @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } //add your switch statement return super.onOptionsItemSelected(item); } 
0
Apr 08 '17 at 13:20
source share

I also had a similar problem, in my case the problem was, when the actionbartoggle started, I did not pass a valid toolbar argument (the toolbar was initialized later), without a valid, non-zero toolbar, ActionBarToggle will not be able to create a hamburger icon.

 actionBarToggle = ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); 
0
Jun 08 '17 at 8:46 on
source share



All Articles