Search code examples
androidonitemclicklistenerandroid-navigationview

Neither onNavigationItemSelected nor onOptionsItemSelected is called


I have a double-drawer layout set up and am trying to handle selecting items inside the drawers. The way I have it set up though, I can see that neither onNavigationItemSelected nor onOptionsItemSelected is getting called when I tap something on the menu (I put a log statement right inside the functions). I'm also not totally clear which of those functions should be called.

EDIT: It looks like onOptionsItemSelected() is being called after all, but whichever item I click on, I always get the same id. So maybe it's only allowing me to click some other layer?

There are a lot of other questions similar to this, but none of the answers have been helpful to me. I'd appreciate any insight you may have.

Here is the relevant code inside my onCreate() function in MainActivity.java:

    // Adding Toolbar to Main screen
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView leftNavigationView = (NavigationView) findViewById(R.id.notifications_view);
    leftNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Log.i("left", "left"); // this is never called
            // Handle left navigation view item clicks here
            int id = item.getItemId();

            switch(id) {
                case R.id.emerg_con_menuitem:
                    break;
                case R.id.ride_hist_menuitem:
                    Log.i("I'm hit", "I'm hit");
                    intent = new Intent(MainActivity.this, loc_report.class); // The action I want to happen when this menu item is tapped
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.settings_menuitem:
                    break;
            }

            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    });

    NavigationView rightNavigationView = (NavigationView) findViewById(R.id.nav_view);
    rightNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Log.i("right", "right"); // this is also never called
            // Handle Right navigation view item clicks here.
            int id = item.getItemId();

            Log.i("I am firing", "I am firing");

            switch(id) {
                case R.id.emerg_con_menuitem:
                    break;
                case R.id.ride_hist_menuitem:
                    Log.i("I'm hit navigation", "I'm hit navigation");
                    intent = new Intent(MainActivity.this, loc_report.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.settings_menuitem:
                    break;
            }

            drawer.closeDrawer(GravityCompat.END);
            return true;
        }
    });

    // Adding menu icon to Toolbar
    ActionBar supportActionBar = getSupportActionBar();
    if (supportActionBar != null) {
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_notifications);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setDisplayShowTitleEnabled(false);
    }

And the onOptionsItemSelected() that is being called, although not on the correct menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    Log.i("I'm hit options", "I'm hit options"); // also never called...what is going on??
    int id = item.getItemId();

    switch(id) {
        case R.id.emerg_con_menuitem:
            break;
        case R.id.ride_hist_menuitem:
            Log.i("I'm hit options", "I'm hit options");
            Intent intent = new Intent(MainActivity.this, loc_report.class);
            this.startActivity(intent);
            break;
        case R.id.settings_menuitem:
            break;
        case R.id.menu_navigation:
            drawer.openDrawer(GravityCompat.END); /*Opens the Right Drawer*/
            return true;
    }

    return super.onOptionsItemSelected(item);
}

Here is the main view activity_mail.xml:

<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"
android:background="@drawable/main_background"
style="@style/Theme.AppCompat.DayNight">

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Protectors"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/Protectors"
            android:textAlignment="center">

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

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navheader"
    app:menu="@menu/menu_navigation" />

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

The main menu with the clickable icon to open the drawer:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MainActivity">

    <group android:checkableBehavior="single">

        <item // this is the item that's being called on a click
            android:id="@+id/menu_navigation"
            android:icon="@drawable/ic_menu"
            android:title="@string/action_notifications"
            android:orderInCategory="100"
            app:showAsAction="always" />

    </group>
</menu>

and the stuff inside the drawer (the right-hand one):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/emerg_con_menuitem"
        android:title="Emergency Contacts"
        />
    <item
        android:id="@+id/ride_hist_menuitem"
        android:title="Ride History" />
    <item
        android:id="@+id/settings_menuitem"
        android:title="Settings" />
</menu>

Solution

  • Ok, I think I figured this out. I'm still having trouble with the right-drawer links, but I got the left-drawer links to work. It was a layering problem--so I was clicking on the main_menu item (which contains the clickable icon that makes the drawer open) instead of the menu items underneath. To fix this, I added this line of code to fix the order:

    NavigationView leftNavigationView = (NavigationView) findViewById(R.id.nav_view);
    leftNavigationView.bringToFront(); // <- added this line
    

    And then the OnNavigationItemSelectedListener actually fires as expected.