Navigation view items will not respond when pressed
The line
NavigationUI.setupWithNavController(navigationView, navController);
Calls setNavigationItemSelectedListener
internally to connect destinations to menu items (i.e., when you click on the R.id.nav_settings
MenuItem, it'll replace the Fragment in your NavHostFragment with the one with android:id="@+id/nav_settings"
set). This listener overrides the OnNavigationItemSelectedListener
view you've set, which is why your custom logic doesn't run.
If you want to combine both sets of functionality together, you need to call navigationView.setNavigationItemSelectedListener(this);
after setupWithNavController
and trigger the default behavior with NavigationUI.onNavDestinationSelected()
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_history, R.id.nav_settings,
R.id.nav_help, R.id.nav_signout)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
// This line needs to be after setupWithNavController()
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
switch (menuItem.getItemId()){
case R.id.nav_history:
Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
break;
case R.id.nav_signout:
signOut();
break;
default:
// Trigger the default action of replacing the current
// screen with the one matching the MenuItem's ID
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Navigation View Menu Item Doesn't respond to any Click
To use a DrawerLayout
, position your primary content view as the first child with width and height of match_parent
and no <layout_gravity>
<androidx.drawerlayout.widget.DrawerLayout
...>
<!-- main content goes here -->
<!-- NavigationView -->
<com.google.android.material.navigation.NavigationView
android:layout_gravity="start|left"
../>
</androidx.drawerlayout.widget.DrawerLayout>
Navigation drawer item click listener not working
You could follow two approach for this.
First approach
would be to use the setOnMenuItemClickListener when you want to implement listener only for a single item in the navigation drawer. This adds a listener for a single item in the navigation drawer without affecting the other navigation items.
val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView
navigationView.menu!!.findItem(R.id.nav_logout).setOnMenuItemClickListener { menuItem:MenuItem? ->
//write your implementation here
//to close the navigation drawer
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
}
Toast.makeText(applicationContext, "single item click listener implemented", Toast.LENGTH_SHORT).show()
true
}
Second Approach
would be to use the setNavigationItemSelectedListener when you want to write listener for each item in the navigation drawer.
val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.nav_gallery -> {
//write your implementation here
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
}
true
}
else -> {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
}
false
}
}
}
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.
Side Navigation Drawer Item click is not working although Navigation Drawer is visible and opens
Your code looks all good, weird it doesn't work but you can try and add
navView.bringToFront();
navView.setNavigationItemSelectedListener(this)
Side Navigation Drawer Item click not working
Just put your NavigationView
after FrameLayout
. Check below code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/CustomTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:itemBackground="@color/colorPrimary"
android:layout_gravity="bottom"
app:itemIconTint="@drawable/selector_bottom_nav_bar"
app:itemTextColor="@drawable/selector_bottom_nav_bar"
app:menu="@menu/navigation"/>
</FrameLayout>
<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>
Related Topics
Failed to Import New Gradle Project: Failed to Find Build Tools Revision *.0.0
How to Sort Alphabetically While Ignoring Case Sensitive
Android Days Between Two Dates
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Android: How to Disable List Items on List Creation
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
How to Automatically Generate Getters and Setters in Eclipse
Using Client/Server Certificates for Two Way Authentication Ssl Socket on Android
Dynamic Listview in Android App
How to Get Timezone from Android Mobile
Onactivityresult() Not Called in New Nested Fragment API
How to Send Data from Dialogfragment to a Fragment
How to Catch a Firebase Auth Specific Exceptions