1

I am implementing bottom navigation bar with activities. I have created a selector which changes the color of the icon when clicked. But when I press the back button, it goes to the previous activity but the color of the icon does not change.

this is my selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_checked="true"
        android:color="@color/colorPrimary"/>
    <item
        android:state_checked="false"
        android:color="@color/grey"/>

</selector>

one of the activity using bottom navigation. All the activity have bottom navigation bar

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChatActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_b_navigation" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="243dp"
        android:layout_marginEnd="149dp"
        android:text="Chat activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Doing this in every activity which is using the bottom navigation bar.

bottom_navigation.selectedItemId = R.id.home

        bottom_navigation.setOnNavigationItemSelectedListener {
            when (it.itemId){
                R.id.home ->{
                    true
                }

                R.id.sell-> {
                    val intent = Intent(this, SellActivity::class.java)
                    startActivity(intent)
                    overridePendingTransition(0,0)
                    true
                }

                R.id.chat -> {
                    val intent = Intent(this, ChatActivity::class.java)
                    startActivity(intent)
                    overridePendingTransition(0,0)
                    true
                }

                R.id.profile -> {
                    val intent = Intent(this, ProfileActivity::class.java)
                    startActivity(intent)
                    overridePendingTransition(0,0)
                    true
                }
                else -> {
                    false
                }
            }
        }

1 Answers1

0

Add your selector below code and try if that works:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_checked="true"
        android:color="@color/colorPrimary"/>
    <item
        android:state_checked="false"
        android:color="@color/grey"/>
    <item
        android:state_selected="true"
        android:color="@color/colorPrimary"/>
    <item
        android:state_selected="false"
        android:color="@color/grey"/>

</selector>
Mert
  • 904
  • 4
  • 9
  • 21