Onnavigationitemselected Not Working In Navigationview
Solution 1:
Use this code:
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.bringToFront();
Solution 2:
Have a look at your MainActivity.java
.
You have implemented the callbacks for NavigationView.OnNavigationItemSelectedListener
in MainActivity
as below,
@OverridepublicbooleanonNavigationItemSelected(MenuItem menuItem) {
// blah blah
}
Also check the setupDrawerContent
method.
privatevoidsetupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
newNavigationView.OnNavigationItemSelectedListener() {
@OverridepublicbooleanonNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
returntrue;
}
});
}
In this method you are creating a local OnNavigationItemSelectedListener
.
So you are not using the OnNavigationItemSelectedListener
that you have overridden in MainActivity
.
The solution is to use this
as argument for setNavigationItemSelectedListener
. By doing this all your clicks will go the onNavigationItemSelected
of MainActivity
rather than going to the local onNavigationItemSelected
.
privatevoidsetupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(this);
}
Also move the code in the local onNavigationItemSelected
to the onNavigationItemSelected
of MainActivity
.
So your onNavigationItemSelected
will be something like this,
@OverridepublicbooleanonNavigationItemSelected(MenuItem menuItem) {
// Handle navigation view item clicks here.intid= menuItem.getItemId();
menuItem.setChecked(true);
drawerLayout.closeDrawers();
if (id == R.id.nav_home) {
// Handle the home action
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
} elseif (id == R.id.nav_the_wetlands) {
Toast.makeText(this, "The Wetlands", Toast.LENGTH_SHORT).show();
TheWetlandsFragmenttheWetlandsFragment=newTheWetlandsFragment();
FragmentManagerfragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relativelayout_for_fragment, theWetlandsFragment, theWetlandsFragment.getTag()).commit();
} elseif (id == R.id.nav_the_mistbelt_forests) {
Toast.makeText(this, "The Mistbelt Forests", Toast.LENGTH_SHORT).show();
}
DrawerLayoutdrawer= (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
returntrue;
}
Also change your activity_main_drawer_view.xml
as follows to solve the multiple selection issue you have in the Navigation Drawer,
<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><groupandroid:checkableBehavior="single"><itemandroid:id="@+id/nav_home"android:icon="@drawable/ic_dashboard"android:title="Home" /></group><itemandroid:title="Information"><groupandroid:checkableBehavior="single"><itemandroid:id="@+id/nav_the_wetlands"android:icon="@drawable/ic_event"android:title="The Wetlands" /><itemandroid:id="@+id/nav_the_mistbelt_forests"android:icon="@drawable/ic_event"android:title="The Mistbelt Forests" /><itemandroid:id="@+id/nav_the_grasslands"android:icon="@drawable/ic_event"android:title="The Grasslands" /></group></item><itemandroid:title="Quick Go To"><groupandroid:checkableBehavior="single"><itemandroid:id="@+id/nav_accommodation"android:icon="@drawable/ic_event"android:title="Accommodation" /><itemandroid:id="@+id/nav_cuisine"android:icon="@drawable/ic_forum"android:title="Cuisine" /><itemandroid:id="@+id/nav_leisure_activites"android:icon="@drawable/ic_forum"android:title="Leisure & Activites" /><itemandroid:id="@+id/nav_agri_tourism"android:icon="@drawable/ic_forum"android:title="Agri-tourism" /><itemandroid:id="@+id/nav_education"android:icon="@drawable/ic_forum"android:title="Education" /><itemandroid:id="@+id/nav_arts_crafts_decor"android:icon="@drawable/ic_forum"android:title="Arts, Crafts & DeCor" /><itemandroid:id="@+id/nav_selective_shopping"android:icon="@drawable/ic_forum"android:title="Selective Shopping" /><itemandroid:id="@+id/nav_for_children"android:icon="@drawable/ic_forum"android:title="For Children" /></group></item><itemandroid:title="Midlands Animals"><groupandroid:checkableBehavior="single"><itemandroid:id="@+id/nav_midlands_birding_checklist"android:icon="@drawable/ic_dashboard"android:title="Midlands Birding Checklist" /><itemandroid:id="@+id/nav_midlands_mammals_checklist"android:icon="@drawable/ic_dashboard"android:title="Midlands Mammals Checklist" /></group></item></menu>
Good luck.
Solution 3:
Don't use
NavigationUI.setupWithNavController(navigationView, navController);
instead of it do this
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
returnfalse;
}
});
Solution 4:
I have also faced the same problem some time back, and at the end i realized that i have not wrote the 2nd line of the following code
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
you make sure you have written the same otherwise your listener will not work
Solution 5:
in my case i forgot to initialize navigation menu.
kindly follow following code:
publicvoidinitSideMenu() {
Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayoutdrawer= (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggletoggle=newActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationViewnavigationView= (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
happy coding...
Post a Comment for "Onnavigationitemselected Not Working In Navigationview"