'How to make bottom nav view items reselectable? Android
For some reason i can't reselect items in my bottom nav view. How can i enable this feauture?
I have 3 top destination fragments (bottom nav view items): A, B, C
App structure looks like this:
A -> A1
B -> B1 -> B2
C
When user is at fragment B2 he should be able to reselect currently active item, and open fragment B.
Here is related code snippets:
mainActivity.kt
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.graph_profile, R.id.graph_tools_library,
R.id.graph_settings
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(
navController)
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="@id/graph_profile">
<navigation
android:id="@+id/graph_profile"
app:startDestination="@id/navigation_profile">
<fragment
android:id="@+id/navigation_profile"
android:name="com.example.pocketpsy.presentation.profile.ProfileFragment"
android:label="@string/profile"
tools:layout="@layout/fragment_profile">
<action
android:id="@+id/navigate_to_results"
app:destination="@id/navigation_results" />
</fragment>
<fragment
android:id="@+id/navigation_results"
android:name="com.example.pocketpsy.presentation.results.ResultsFragment"
android:label="@string/results">
<argument
android:name="resultsID"
app:argType="string" />
</fragment>
</navigation>
<navigation
android:id="@+id/graph_tools_library"
app:startDestination="@id/navigation_tools_library">
<fragment
android:id="@+id/navigation_tools_library"
android:name="com.example.pocketpsy.presentation.toolslibrary.ToolsLibraryFragment"
android:label="@string/tools_library"
tools:layout="@layout/fragment_tools_library">
<action
android:id="@+id/navigate_to_description"
app:destination="@id/navigation_description" />
<action
android:id="@+id/navigate_to_tool"
app:destination="@id/navigation_tool" />
</fragment>
<fragment
android:id="@+id/navigation_description"
android:name="com.example.pocketpsy.presentation.description.DescriptionFragment">
<argument
android:name="toolID"
app:argType="string" />
<action
android:id="@+id/navigate_to_tool"
app:destination="@id/navigation_tool"
app:popUpTo="@id/navigation_tools_library" />
</fragment>
<fragment
android:id="@+id/navigation_tool"
android:name="com.example.pocketpsy.presentation.tool.ToolFragment"
android:label="@string/tool"
tools:layout="@layout/fragment_tool">
<argument
android:name="toolID"
app:argType="string" />
<action
android:id="@+id/navigate_to_results"
app:destination="@id/navigation_results" />
</fragment>
<fragment
android:id="@+id/navigation_results"
android:name="com.example.pocketpsy.presentation.results.ResultsFragment"
android:label="@string/results">
<argument
android:name="resultsID"
app:argType="string" />
</fragment>
</navigation>
<navigation
android:id="@+id/graph_settings"
app:startDestination="@id/navigation_settings">
<fragment
android:id="@+id/navigation_settings"
android:name="com.example.pocketpsy.presentation.settings.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings" />
</navigation>
</navigation>
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/graph_profile"
android:icon="@drawable/ic_bottom_bar_profile"
android:title="A" />
<item
android:id="@+id/graph_tools_library"
android:icon="@drawable/ic_bottom_bar_tools_library"
android:title="B"/>
<item
android:id="@+id/graph_settings"
android:icon="@drawable/ic_bottom_bar_settings"
android:title="C" />
</menu>
Solution 1:[1]
if you want to unselect specific manu item in nav View the use
navigationView.getMenu().getItem(3).setChecked(false);
if use this in mainActivity or homeactivity and on position 0 item then on start of app there is no menu selected by default
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adnan Bashir |
