onOptionsItemSelected not called in activity

1k Views Asked by At

Here's my problem. The onOptionsItemSelected() not called on drawer click. So here's my code, that's very simple...

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private RelativeLayout aboutUs, feedBack;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = findViewById(R.id.mainView);
    aboutUs = (RelativeLayout) findViewById(R.id.list4);
    feedBack = (RelativeLayout) findViewById(R.id.list5);

    initToolbar();
    drawerInit();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        if (drawer.isDrawerOpen(Gravity.LEFT)) {
            drawer.closeDrawer(Gravity.LEFT);
        } else {
            drawer.openDrawer(Gravity.LEFT);
        }
    }
    return super.onOptionsItemSelected(item);
}

public void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

public void drawerInit() {
    drawer = (DrawerLayout) findViewById(R.id.drawer);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            float moveFactor = (drawerView.getWidth() * slideOffset);
            view.setTranslationX(moveFactor);
        }

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

    drawer.addDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

}

I set breakpoint in optionsItemSelected callback, but that not works on drawer click.

But the drawer opens. So where's the problem and how can I fix it? Thanks for answer.....................

1

There are 1 best solutions below

0
On

This activity.xml code..

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" />

  </android.support.v4.widget.DrawerLayout>