How to change INACTIVE icons of BottomNavigationView

Viewed 54

I have four BottomNavigationView items, Let's say A, B, C, and D. I have two icons for all, which is applied like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/home_icon" android:state_checked="false"/>
<item android:drawable="@mipmap/home_selected" android:state_checked="true"/>
</selector>

My menu.xml

 <item
    android:id="@+id/navigation_home"
    android:icon="@drawable/home_selection"
    android:title="@string/title_home" />

What I want is when I click C, the icons of A, B, and D should be changed to a Third Icon respectively. Hope this makes sense. Is there a way to achieve this?

Maybe there is a way to add a third icon in the selector.xml but I don't know which state should be applied.

1 Answers

Solved, if someone having the same problem, try this:

bottom_menu.xml

<?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/home_selection"
    android:title="@string/title_home" />

<item
    android:id="@+id/navigation_meditate"
    android:icon="@drawable/meditate_selection"
    android:title="@string/title_meditate" />

<item
    android:id="@+id/navigation_sleep"
    android:icon="@drawable/sleep_selection"
    android:title="@string/title_sleep" />
<item
    android:id="@+id/navigation_sounds"
    android:icon="@drawable/sound_selection"
    android:title="@string/title_sounds" />

</menu>

and your bottomNavigationItemListener should look like this:

   private final BottomNavigationView.OnNavigationItemSelectedListener 
   mOnNavigationItemSelectedListener
        = item -> {
    switch (item.getItemId()) {

        case R.id.navigation_home:
           
            navigation.setBackgroundResource(R.color.colorWhite);

            navigation.getMenu().getItem(0).setIcon(R.mipmap.home_selected);                navigation.getMenu().getItem(1).setIcon(R.mipmap.meditate_icon);                navigation.getMenu().getItem(3).setIcon(R.mipmap.songs_icon);
            navigation.setItemTextColor(textColorStatesBlack);


            return true;
        case R.id.navigation_meditate:

           navigation.setBackgroundResource(R.color.colorWhite);
            navigation.getMenu().getItem(0).setIcon(R.mipmap.home_icon);              navigation.getMenu().getItem(1).setIcon(R.mipmap.meditate_selected);                navigation.getMenu().getItem(3).setIcon(R.mipmap.songs_icon);
            navigation.setItemTextColor(textColorStatesBlack);
            
            return true;
        case R.id.navigation_sleep: 

   navigation.setBackgroundResource(R.color.sleepModeNavColor);               
   navigation.getMenu().getItem(0).setIcon(R.mipmap.home_sleep_icon);            
navigation.getMenu().getItem(1).setIcon(R.mipmap.meditate_sleep_icon);
navigation.getMenu().getItem(3).setIcon(R.mipmap.sounds_sleep_icon);
            navigation.setItemTextColor(textColorStatesSleep);
         

            return true;
        case R.id.navigation_sounds:
        navigation.setItemTextColor(textColorStatesBlack);
        navigation.getMenu().getItem(0).setIcon(R.mipmap.home_icon);                
    navigation.getMenu().getItem(1).setIcon(R.mipmap.meditate_icon);             
    navigation.getMenu().getItem(3).setIcon(R.mipmap.sound_selected);
            clParent.setBackgroundResource(R.color.colorWhite);
            navigation.setBackgroundResource(R.color.colorWhite);
          
            return true;
    }

    return false;
};

for text colors:

 ColorStateList textColorStatesSleep = new ColorStateList(
        new int[][]{
                new int[]{-android.R.attr.state_checked},
                new int[]{android.R.attr.state_checked}
        },
        new int[]{
                Color.parseColor("#8989bc"),
                Color.parseColor("#8989bc")
        });
ColorStateList textColorStatesBlack = new ColorStateList(
        new int[][]{
                new int[]{-android.R.attr.state_checked},
                new int[]{android.R.attr.state_checked}
        },
        new int[]{
                Color.parseColor("#000000"),
                Color.parseColor("#000000")
        });

This will work perfectly in the above scenario. If there is anything missing or not understandable, comment below.

Related