I changed toolbar hamburger icon but it does not open drawer menu when I clicked

I am using android toolbar instead of action bar as you can see below inside code. I changed hamburger icon with what my clients want. But drawer menu does not open when I clicked on icon. Here is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    toolbar=(Toolbar)findViewById(R.id.tool_bar);
    toolbar.inflateMenu(R.menu.menu_event_list);
    toolbar.setNavigationIcon(R.drawable.icon_menu);
    toolbar.setOnMenuItemClickListener(menuClicked);
    toolbar.setTitle(getResources().getString(R.string.app_name));

    mRecyclerView=(RecyclerView)findViewById(R.id.RecyclerView);
    mRecyclerView.setHasFixedSize(true);

    mAdapter=new MenuAdapter(titles,icons,u.fullName,u.email,R.drawable.a,this);
    mRecyclerView.setAdapter(mAdapter);

    mLayoutManager=new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    Drawer=(DrawerLayout)findViewById(R.id.DrawerLayout);
    mDrawerToggle=new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.app_name,R.string.app_name){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
            // open I am not going to put anything here)
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            // Code here will execute once drawer is closed
        }
    };
    mDrawerToggle.setHomeAsUpIndicator(R.drawable.icon_menu);
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    Drawer.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
}

and xml for this

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        />

</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/RecyclerView"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="#ffffff"
    android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

Upvotes: 2

Views: 740

Answers (2)

Arvis
Arvis

Reputation: 8363

Missing some Overrides in the Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }

    return super.onOptionsItemSelected(item);
}

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

Upvotes: 0

Steve
Steve

Reputation: 826

According to the ActionBarDrawerToggle documentation:

syncState() should be called from your Activity's onPostCreate method to synchronize after the DrawerLayout's instance state has been restored, and any other time when the state may have diverged.

Consider moving your call to syncState() to onPostCreate after the state has settled.

Upvotes: 2

Related Questions