Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onNavigationItemSelected is not called

I want to call onNavigationItemSelected when someone clicks on navigation view. The code doesn't call the onNavigationItemSelected method:

 public boolean onNavigationItemSelected(MenuItem menuItem) {
    Log.d("Hello","called navigation");

    switch (menuItem.getItemId()) {
        case R.id.home:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;
        default:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;

    }

    return false;
}

The activity_main.xml is:

<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"
tools:context="slidenerd.vivz.navigationviewdemo.MainActivity">

<LinearLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />



</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/main_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:itemIconTint="@color/colorAccent"
    app:itemTextColor="@color/colorTextSecondary"
    app:menu="@menu/menu_drawer" />

The navigation_menu is:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!--All the items added without any grouping-->
    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_home"
        android:title="@string/home" />

    <item
        android:id="@+id/statistics"
        android:icon="@drawable/ic_trending_up"
        android:title="@string/statistics" />

    <item
        android:id="@+id/navigation_item_3"
        android:icon="@drawable/ic_pencil"
        android:title="@string/navigation_item_3" />
 </menu>

But, the code is not working. Please help me to solve the problem.

like image 432
learner Avatar asked Feb 17 '16 20:02

learner


2 Answers

You should set the NavigationView.setNavigationItemSelectedListener()

like image 108
Farhad Faghihi Avatar answered Sep 23 '22 20:09

Farhad Faghihi


does your activity implements NavigationView.OnNavigationItemSelectedListener??

if it does, it make sense that you need the method: onNavigationItemSelected

try to add to your onCreate the command: navigationView.setNavigationItemSelectedListener(this);

if the NavigationView is called navigationView.

after you find it from your activity with findViewById

like image 25
Zeev Mindali Avatar answered Sep 25 '22 20:09

Zeev Mindali