Dennis Vash
Dennis Vash

Reputation: 53874

A good approach using BottomNavigationView?

I want to navigate between activities while coloring the current item on bottom navigation.

What is the best approach to achieve this?

Currently, on every activity when I'm using BottomNavigationView (in this casecom.google.android.material.bottomnavigation.BottomNavigationView),

I'm duplicating almost the same code for navigating between activities which is hard to maintain:

  1. Marking the current itemId
  2. Setting thesetOnNavigationItemSelectedListener without the current itemId.

    jesta_bottom_navigation.selectedItemId = R.id.nav_do_jesta
    
    jesta_bottom_navigation.setOnNavigationItemSelectedListener {
    
        val intent = when (it.itemId) {
            R.id.nav_ask_jesta -> {
                Intent(this@DoJestaActivity, AskJestaActivity::class.java)
            }
            R.id.nav_status -> {
                Intent(this@DoJestaActivity, StatusActivity::class.java)
            }
            // Settings Activity
            else -> {
                Intent(this@DoJestaActivity, SettingsActivity::class.java)
            }
        }
        startActivity(intent)
        true
    }
    

frame_bottom_navigation_view.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/jesta_bottom_navigation"
        style="@style/Widget.Jesta.BottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_bar"
        android:layout_gravity="bottom"
        app:itemIconSize="30dp"
        app:itemIconTint="@drawable/jesta_bottom_navigation_colors"
        app:labelVisibilityMode="unlabeled"
        app:menu="@menu/bottom_nav_drawer_menu" />

</FrameLayout>

Upvotes: 0

Views: 93

Answers (1)

Mohammed Mahmoud
Mohammed Mahmoud

Reputation: 1148

What about using an external library for doing this faster

Upvotes: 1

Related Questions