BottomNavigationBar-change the tab icon color
Asked Answered
S

6

26

I've integrated Bottom Bar Navigation bar on my app. But when I swipe, tab's color doesn't change. It's weird cause I have selector file. Any idea to solve this problem?

Activity.java

BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.bottom_navigation);


    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.bb_menu_arac:
                            fragment = new AraclarimFragment();
                            break;
                        case R.id.bb_menu_yaklasan:
                            fragment = new YaklasanlarFragment();
                            break;
                        case R.id.bb_menu_yakin:
                            fragment = new Yakinimdakiler();
                            break;

                    }
                    final FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.main_container, fragment).commit();
                    return true;
                }


            });

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/beyaz" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>

activiy.xml

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/beyaz"
    app:itemTextColor="@color/beyaz"
    app:menu="@menu/bottombar_menu" />
Stridulous answered 20/2, 2017 at 14:14 Comment(4)
Where have you used selector.xml?Substation
You've written in code that androida:state_enabled instead of android:state_enabled ?? Is that making problem?Kaiser
@AnuragSingh ActualIy, I have no idea where should I use or call selector.xmlStridulous
@YunusHaznedar you want to change text color for selected item or background of selected item?Kaiser
S
63

Change to app:itemIconTint="@drawable/selector"

Also change your selector.xml to this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/beyaz" />
<item android:color="@color/colorPrimaryDark"  />
</selector>
Substation answered 20/2, 2017 at 14:37 Comment(1)
changing android:color to android:drawable was the point for mePerceptible
N
11

I was confused about whole process despite reading all the answers, so here is how I resolved it step by step so beginners can understand it properly

Let's say you created MainActivity with bottom navigation so

in your drawable folder create bottom_navigation_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:color="@color/yourSelectedColor" />
  <item android:color="@color/defaultColor"  />
</selector>

then go to the activity_main.xml layout and add this line in BottomNavigationView

app:itemIconTint="@drawable/bottom_navigation_selector"

If you also want to change text color accordingly then you need to add this line aswell

app:itemTextColor="@drawable/bottom_navigation_selector"
    
Nickey answered 30/12, 2020 at 7:23 Comment(0)
O
4

You have to set selector as itemIconTint of your BottomNavigationView. Something like

 <android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/selector"
    app:itemTextColor="@color/beyaz"
    app:menu="@menu/bottombar_menu" />
Overmatch answered 20/2, 2017 at 14:34 Comment(0)
R
1

I use way @Anurag Singh, but it didn't work until I removed this line:

navigation?.itemIconTintList = null

Thank myself.

Rickierickman answered 3/6, 2022 at 9:58 Comment(0)
M
1

Just add: item.setChecked(true);

inside each case;

Mateya answered 25/8, 2022 at 12:26 Comment(0)
S
1

In BottomNavigationView app:itemIconTint="" provide color not selector

the solution below works for me.

bottomNavigationView = findViewById(R.id.bottomNavView);
int[][] states = new int[][] {
        new int[] { android.R.attr.state_selected},
        new int[] {-android.R.attr.state_selected},
        new int[] {-android.R.attr.state_checked},
        new int[] { android.R.attr.state_checked}
};
int[] colors = new int[] {
      ContextCompat.getColor(this,R.color.colorPrimary),
      ContextCompat.getColor(this,R.color.gray),
      ContextCompat.getColor(this,R.color.gray),
      ContextCompat.getColor(this,R.color.colorPrimary)
};
ColorStateList myList = new ColorStateList(states, colors);
bottomNavigationView.setItemIconTintList(myList);//this for item icon 
bottomNavigationView.setItemTextColor(myList);// this for item text

create ColorStateList and add tint color programmatically

Selfhood answered 9/12, 2023 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.