NavigationView OnNavigationItemSelectedListener not being called
Asked Answered
C

3

18

I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code

Activity Layout

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu" />

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

Activity onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutID());

    toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.common_menu);
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            // allow some time after closing the drawer before performing real navigation
            // so the user can see what is happening
            drawerLayout.closeDrawer(GravityCompat.START);
            mDrawerActionHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    navigate(menuItem.getItemId());
                }
            }, DRAWER_CLOSE_DELAY_MS);
            drawerLayout.closeDrawers();
            return true;
        }

    });
    usernameTextView = (TextView) findViewById(R.id.drawer_header_username);                  
    usernameTextView.setText(getAppDContext().getAccount().getUsername());
   }
Companion answered 14/7, 2015 at 4:41 Comment(3)
Is there a reason for setting the listener twice on the NavigationView?Rocray
Oops. Pasted wrong code here. Corrected. Thanks for pointing it out.Companion
Post your full layoutOfficious
W
70

When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)

<DrawerLayout>
    <FrameLayout />
    <NavigationView />
</DrawerLayout>
Whitmore answered 16/7, 2015 at 6:46 Comment(1)
This fixed a problem I had when exiting a full screen video in a WebView using the back button. The FrameLayout was listed after the NavigationView in my XML file, and even though it was set as visibility GONE it was still covering up the NavigationView and preventing touches from being registered.Chintzy
A
16

For me this did the trick!

navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
Alpenglow answered 20/3, 2017 at 15:19 Comment(0)
B
7

Your activity main layout should look like this:

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

    <include layout="@layout/activity_main_content" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        style="@style/NavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        app:headerLayout="@layout/header"
        app:menu="@menu/menu_drawer"/>

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

In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder) for example menu_drawer.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav1"
                android:checked="true"
                android:icon="@drawable/logo"
                android:title="Navigation item 1"/>
            <item
                android:id="@+id/nav2"
                android:icon="@drawable/logo"
                android:title="Navigation item 2"/>
        </group>
    </menu>

than your java code:

public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

        setUpToolbar();
        setUpNavDrawer();
    }

 private void setUpNavDrawer() {
        NavigationView view = (NavigationView) findViewById(R.id.navigationView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
        view.setNavigationItemSelectedListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
    }

Check if this work for you. In my project works like a charm.

Burushaski answered 14/7, 2015 at 9:17 Comment(1)
Does it call onOptionsItemSelected when hamburger icon is selected? No. Also when I select a navigation item it is not calling the listenerCompanion

© 2022 - 2024 — McMap. All rights reserved.