Android Bottom Navigation View

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"
android:icon="@drawable/ic_browse"
android:title="@string/browse" />

<item
android:id="@+id/menu_navigation_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />

<item
android:id="@+id/menu_navigation_premium"
android:icon="@drawable/ic_premium"
android:title="@string/premium_title" />

</menu>

activity_home.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
style="@style/BottomNavigationStyle"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_navigation_bar_height"
android:layout_gravity="bottom"
android:background="@color/ink"
android:visibility="gone"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_navigation_menu" />

Thanks for reading and let me know if I’m wrong somewhere or if there’s something that could do differently or better. I’m open to your feedback 🙌🏻

-Andres

software engineer, read, swim, travel. apps: https://goo.gl/rDuuQw https://goo.gl/15GvfE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store