NavigationView OnNavigationItemSelectedListener not being called
When you make XML layout, you should write down NavigationView
after BaseLayout (FrameLayout
, LinearLayout
, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>
NavigationView onNavigationItemSelected not working
You can't use NavigationUI.setupWithNavController()
and setNavigationItemSelectedListener()
together.
onNavigationItemSelected
will work if you remove this line from your code:
NavigationUI.setupWithNavController(navigationView, navController);
but then you have to handle fragment transactions manually.
NavigationView onNavigationItemSelected is not called
So, the problem was cause by creating new activity for logging user.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_with_nav)
val mNavigationView = findViewById<View>(R.id.nav_view) as NavigationView
mNavigationView.setNavigationItemSelectedListener(this)
//start login action
// In the future I will check here some session, but for debbuging I want to log every time
if(true){
val intent = Intent(applicationContext, LoginActivity::class.java)
startActivity(intent)
}
After loggin i changed view to the main view but didn't finish that login activity. So all I had to do call finish() after changing view back to main view.
activity.setContentView(R.layout.activity_main_with_nav);
activity.finish()
onNavigationItemSelected not working in NavigationView
Have a look at your MainActivity.java
.
You have implemented the callbacks for NavigationView.OnNavigationItemSelectedListener
in MainActivity
as below,
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// blah blah
}
Also check the setupDrawerContent
method.
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
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
.
private void setupDrawerContent(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,
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Handle navigation view item clicks here.
int id = 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();
} else if (id == R.id.nav_the_wetlands) {
Toast.makeText(this, "The Wetlands", Toast.LENGTH_SHORT).show();
TheWetlandsFragment theWetlandsFragment = new TheWetlandsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relativelayout_for_fragment, theWetlandsFragment, theWetlandsFragment.getTag()).commit();
} else if (id == R.id.nav_the_mistbelt_forests) {
Toast.makeText(this, "The Mistbelt Forests", Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
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"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_dashboard"
android:title="Home" />
</group>
<item android:title="Information">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_the_wetlands"
android:icon="@drawable/ic_event"
android:title="The Wetlands" />
<item
android:id="@+id/nav_the_mistbelt_forests"
android:icon="@drawable/ic_event"
android:title="The Mistbelt Forests" />
<item
android:id="@+id/nav_the_grasslands"
android:icon="@drawable/ic_event"
android:title="The Grasslands" />
</group>
</item>
<item android:title="Quick Go To">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_accommodation"
android:icon="@drawable/ic_event"
android:title="Accommodation" />
<item
android:id="@+id/nav_cuisine"
android:icon="@drawable/ic_forum"
android:title="Cuisine" />
<item
android:id="@+id/nav_leisure_activites"
android:icon="@drawable/ic_forum"
android:title="Leisure & Activites" />
<item
android:id="@+id/nav_agri_tourism"
android:icon="@drawable/ic_forum"
android:title="Agri-tourism" />
<item
android:id="@+id/nav_education"
android:icon="@drawable/ic_forum"
android:title="Education" />
<item
android:id="@+id/nav_arts_crafts_decor"
android:icon="@drawable/ic_forum"
android:title="Arts, Crafts & DeCor" />
<item
android:id="@+id/nav_selective_shopping"
android:icon="@drawable/ic_forum"
android:title="Selective Shopping" />
<item
android:id="@+id/nav_for_children"
android:icon="@drawable/ic_forum"
android:title="For Children" />
</group>
</item>
<item android:title="Midlands Animals">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_midlands_birding_checklist"
android:icon="@drawable/ic_dashboard"
android:title="Midlands Birding Checklist" />
<item
android:id="@+id/nav_midlands_mammals_checklist"
android:icon="@drawable/ic_dashboard"
android:title="Midlands Mammals Checklist" />
</group>
</item>
</menu>
Good luck.
NavigationItemSelected not being called on Navigation Drawer
try to change you xaml like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:minWidth="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:id="@+id/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
onNavigationItemSelected is not called
You should set the NavigationView.setNavigationItemSelectedListener()
onNavigationItemSelected not registering user selection
There are a couple of ways to achieve this.
The easisest one is to:
- In your activity define a function with public access modifier so you can access it outside of your class.
void someMethodOnMainActivity() {}
- In your fragment:
Activity activity = requireActivity();
if (activity instanceof MainActivity) {
((MainActivity) activity).someMethodOnMainActivity();
}
You can also invoke onNavigationItemSelected()
in your MainActivity.
Related Topics
Startforeground Fail After Upgrade to Android 8.1
Android: Remove Notification from Notification Bar
How to Automatically Update Application on Android
Webview Load HTML from Assets Directory
Call Removeview() on the Child's Parent First
Android: How to Get Accurate Altitude
How to Make an Android App Return to the Last Open Activity When Relaunched
Get Application Name from Package Name
How to Use the Simple Http Client in Android
Broadcastreceiver Not Receiving Boot_Completed
Android: How to Check If Activity Is Running
How to Startforeground() Without Showing Notification
Android 4.3 Menu Item Showasaction="Always" Ignored
Force Application to Restart on First Activity