Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NavigationView Not Responding to Click Events on Items

Tags:

java

android

I am trying to add a navigation view to my application, but for some reason I cannot get it to respond to any click events on any of the items it contains.

My activity_main.xml file looks 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/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <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/navheader"
        app:menu="@menu/menu_navigation" />

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

And in my MainActivity.java I have this code for creating the NavigationView and it's item selection listener:

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

    ...

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                // This method will trigger on item Click of navigation menu
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    if(menuItem.isChecked()) menuItem.setChecked(false);
                    else menuItem.setChecked(true);
                    mDrawerLayout.closeDrawers();
                    switch(menuItem.getItemId()) {
                        case R.id.nav_item_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Items Selected",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.nav_logs_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Logs Selected",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.nav_settings_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Settings Selected",Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            return true;
                    }
                    return true;
                }
            });

    final ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

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

    fragmentManager = getSupportFragmentManager();

    ...
}

This problem has really had me scratching my head because I have used this exact same code in older applications I have written and it seemed to work fine. I am using Android SDK 22 with Build Tools 22.0.1.

like image 516
aestamper Avatar asked Apr 23 '16 20:04

aestamper


1 Answers

Had the same issue, Here is my solution.

I was programmatically adding HeaderView to NavigationView Hence I already had NavigationView

I called navigationView.bringToFront();

Here is code snippet for context

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();

Here is this code snippet in GitHub Repo for my Project

like image 111
Suraj Avatar answered Oct 25 '22 22:10

Suraj