ittianyu / BottomNavigationViewEx

An android lib for enhancing BottomNavigationView. 一个增强BottomNavigationView的安卓库。
MIT License
3.47k stars 558 forks source link

How to change onNavigationItemSelected icon color? #167

Open andyleekp opened 5 years ago

andyleekp commented 5 years ago

How to change onNavigationItemSelected icon color?

Poriazy commented 5 years ago

Hello there, i think you can not change the icon color in onNavigationItemSelected @override method. but you can get the selected item text and change the color or typeface like this:

bottomNavView.setOnNavigationItemSelectedListener(newBottomNavigationView.OnNavigationItmSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int selected = bottomNavView.getMenuItemPosition(menuItem);
        bottomNavView.getBottomNavigationItemView(selected). // now you can get the text
        return true;
        }
});

but if you want to change the icon color when an item selected, you can use this way: xml file:

app:itemIconTint="@drawable/color_state_primary_dark"
app:itemTextColor="@drawable/color_state_primary_dark"

and then create new drawable resource and name it color_state_primary_dark:


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

    <item
        android:color="@color/grey_500"
        android:state_checked="false"/>
</selector>