2

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>

2 Answers2

2

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.

Steve
  • 836
  • 1
  • 9
  • 17
  • 1
    I updated the code. It was already in onCreate. I am looking for onPostCreate – Hüseyin Bülbül May 13 '15 at 16:30
  • 1
    It gives nullPointerException when i do it inside onPostCreate here is a little bit of errors 05-13 19:35:21.141 22946-22946/com.mobigo.apps.mobiticket E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.mobigo.apps.mobiticket, PID: 22946 java.lang.NullPointerException at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764) at android.view.View.measure(View.java:16521) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:883) – Hüseyin Bülbül May 13 '15 at 16:37
  • 1
    I solved the error but it still does not open the navigation drawer when i cliked the changed icon – Hüseyin Bülbül May 13 '15 at 16:50
0

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();
}
Arvis
  • 8,273
  • 5
  • 33
  • 46