ductran
ductran

Reputation: 10203

Navigation Drawer icon not showing when opening menu

Below code is Navigation Drawer with toolbar in my app:

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.empty, R.string.empty) {

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
        }
    };

    drawerLayout.setDrawerListener(drawerToggle); // Drawer Listener set to the Drawer toggle
    drawerToggle.syncState();


   @Override
    protected void onPostCreate(Bundle savedInstanceState) {
       super.onPostCreate(savedInstanceState);
       drawerToggle.syncState();
   }

   @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      drawerToggle.onConfigurationChanged(newConfig);
   }

main_activity.xml

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/toolbar" />

</LinearLayout>
<ListView
    android:id="@+id/lvMenu"
    android:background="@color/black"
    android:layout_width="310dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"/>
 </android.support.v4.widget.DrawerLayout>

The menu can show correctly, but when menu is opening, the menu icon is disappear like below image:

enter image description here

And here is normal state:

enter image description here

Is there any idea?

Upvotes: 1

Views: 1583

Answers (1)

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Following code works for me :

  drawerLayout.setDrawerListener(drawerToggle); // Drawer Listener set to the Drawer toggle

  drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            // To display hamburger icon in toolbar
            drawerToggle.syncState();
        }
    });

You need to put drawerToggle.syncState(); inside Runnable() and only add drawerToggle.syncState(); once in code.

Hope this helps.

Upvotes: 1

Related Questions