Is it possible to hide/show the bottom navigation menu item in Kotlin based on the user logged in? I have some items in the bottom navigation which I do not want to show to all the users.
Edit: Here is my code.
I want to remove @+id/navigation_products
from the menu when the users are not, for example, abc and xyz.
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_vector_dashboard"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_products"
android:icon="@drawable/ic_vector_products"
android:title="@string/title_products" />
<item
android:id="@+id/navigation_orders"
android:icon="@drawable/ic_vector_orders"
android:title="@string/title_orders" />
<item
android:id="@+id/navigation_sold_products"
android:icon="@drawable/ic_vector_sold_products"
android:title="@string/title_sold_products" />
<item
android:id="@+id/navigation_orders_by_status"
android:icon="@drawable/ic_vector_sold_products"
android:title="@string/title_orders_by_status" />
</menu>
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/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.ui.fragments.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@+id/navigation_products"
android:name="com.ui.fragments.ProductsFragment"
android:label="@string/title_products"
tools:layout="@layout/fragment_products" />
<fragment
android:id="@+id/navigation_orders"
android:name="com.ui.fragments.OrdersFragment"
android:label="@string/title_orders"
tools:layout="@layout/fragment_orders" />
<fragment
android:id="@+id/navigation_sold_products"
android:name="com.ui.fragments.SoldProductsFragment"
android:label="@string/title_sold_products"
tools:layout="@layout/fragment_sold_products" />
<fragment
android:id="@+id/navigation_orders_by_status"
android:name="com.ui.fragments.OrdersByStatusFragment"
android:label="@string/title_orders_by_status"
tools:layout="@layout/fragment_sold_products" />
</navigation>
DashboardActivity.kt
class DashboardActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
supportActionBar!!.setBackgroundDrawable(
ContextCompat.getDrawable(
this@DashboardActivity,
R.drawable.app_gradient_color_background
)
)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_products,
R.id.navigation_dashboard,
R.id.navigation_orders,
R.id.navigation_sold_products,
R.id.navigation_orders_by_status
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onBackPressed() {
doubleBackToExit()
}
}
You can launch the app with the entire menu items and then remove items based on the logged-in user:
Here is an extension function to the BottonNavigationView
that can remove an item with a certain id
fun BottomNavigationView.removeItem(id: Int) {
if (menu.findItem(id) != null)
menu.removeItem(id)
}
And then you can apply it on the logged-in user:
// check logged-in user
when (loggdUser) {
user_a -> {
bottomNavView.removeItem(R.id.menu_item_id1)
bottomNavView.removeItem(R.id.menu_item_id2)
}
user_b -> bottomNavView.removeItem(R.id.menu_item_id2)
}
menu_item_idx
is supposed menu ids & bottomNavView
is the BottomNavigationView
instance; so replace them to yours
UPDATE:
Testing that in the code:
class DashboardActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
supportActionBar!!.setBackgroundDrawable(
ContextCompat.getDrawable(
this@DashboardActivity,
R.drawable.app_gradient_color_background
)
)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_products,
R.id.navigation_dashboard,
R.id.navigation_orders,
R.id.navigation_sold_products,
R.id.navigation_orders_by_status
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
val loggdUser = "user_a"
// check logged-in user
when (loggdUser) {
"user_a" -> {
navView.removeItem(R.id.navigation_orders_by_status)
navView.removeItem(R.id.navigation_orders)
}
"user_b" -> navView.removeItem(R.id.navigation_orders)
}
}
fun BottomNavigationView.removeItem(id: Int) {
if (menu.findItem(id) != null)
menu.removeItem(id)
}
}