Member-only story

Android Bottom Navigation View

Andres Sandoval
2 min readMay 1, 2020

--

Tutorial shows how to enable/disable visibility of an Android bottom navigation view tap depending on a control flag. This is good when you want to enable a tab only for some users.

Add below Kotlin code snippet to enable or disable the “Premium” tab.

fun onCreate() {
if (!ExperimentSettings.isPremiumTabEnabled()) {
navigationBar.menu.removeItem(R.id.menu_navigation_premium)
}

navigationBar.setOnNavigationItemSelectedListener(this)
}

if: the config flag is true show the “Premium tab”

else: If the config flag is false do not show the “Premium tab”

It took me some hours to figure out to use “navigationBar.menu.removeItem”, instead of using the xml property “android:visibility”:

android:visibility=”gone”

android:visibility=”visible”

android:visibility=”invisible”

Code example:

Bottom navigation tabs — bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/menu_navigation_home"
android:icon="@drawable/ic_home"
android:title="@string/home" />

<item
android:id="@+id/menu_navigation_favorites"
android:icon="@drawable/ic_favorites"
android:title="@string/favorites" />

<item
android:id="@+id/menu_navigation_browse"…

--

--

Andres Sandoval
Andres Sandoval

Written by Andres Sandoval

I'm a passionate Android Software Engineer with over 11 years of experience. andresand.github.io/andres-about-me/ buymeacoffee.com/andresand

No responses yet