Search code examples
androidandroid-activityandroid-menubottomnavigationviewandroid-jetpack-navigation

How to change specific item background in bottom navigation view programmatically?


I want to change background color of last tab inside bottom navigation view. I did a lot of internet search but couldn't found any solution. I am gonna attach my all code here. Please find the image below, which I want to achieve.

Here is my activity code

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
    }
}

Here is my activity xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        app:itemIconTint="@color/tab_selector"
        android:background="@drawable/shape_bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

My menu xml file

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

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="" />

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="" />

    <item
        android:id="@+id/navigation_page"
        android:icon="@drawable/ic_page"
        android:title="" />

    <item
        android:id="@+id/navigation_refresh"
        android:icon="@drawable/ic_refresh"
        android:title="" />

</menu>

enter image description here


Solution

  • Changed background of specific tab inside BottomNavigationView using following code.

    var bottomNavigationMenuView = (bottomNavigationView[0] as BottomNavigationMenuView)
    if (bottomNavigationMenuView.isNotEmpty()) {
        bottomNavigationMenuView[TAB_INDEX].setBackgroundResource(R.drawable.shape_refresh)
    }
    

    In my case TAB_INDEX is 3

    enter image description here