How to Change the Icon Color Selected on Bottom Navigation Bar in Android Studio

How to change the icon color selected on bottom navigation bar in android studio

To change the selected tab icon color in BottomNavigationView you should use the Selector.

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>

Apply app:itemIconTint="@drawable/bottom_navigation_selector" to your BottomNavigationView in xml file.

Change the icon color in a BottomNavigationView

The icon tint for a BottomNavigationView is controlled by the app:itemIconTint attribute, not the tint specified in your menu or drawable resource.

So you should have something like this:

<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemIconTint="@color/navigation_icon_tint" />

If you wanted the colour to change depending on whether it was selected you should use a selector with a checked state instead of a static colour:

<selector>

<item android:state_checked="true" android:color="..." />
<item android:color="..." />
</selector>

For future reference, you can always read about the material components in the documentation at material.io which should cover everything relating to styling and usage.

Android - Bottom Navigation menu selected item's icon color is not changing due to fragments

The issue is here:

    @Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
...
return false; //Use true in your case
}

You can check the doc:

Returns

boolean true to display the item as the selected item and false if the item should not be selected. Consider setting non-selectable items as disabled preemptively to make them appear non-interactive.

Also in you layout use:

<com.google.android.material.bottomnavigation.BottomNavigationView
app:itemTextColor="@color/icon_color"

moving the selector in the res/color folder.

How to set different colors for bottom navigation icons/text in Android

In your selector, in your second item, try adding android:state_checked="false" :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="@color/selected" />
<item
android:state_checked="false"
android:color="@android:color/not_selected" />
</selector>

Android - Change color of icon and title of each tab bottom navigation

Bottom navigation bar overrides icon colors via app:itemIconTint, but removing that attribute from the XML just makes the nav bar use the app's default colors instead. To prevent the bar from applying color changes, and let your selectors work as intended, you have to set the icon tint list to null in code, like this:

bottom_navigation_bar.itemIconTintList = null

EDIT: I see you want to color the text as well, that's a bit trickier. Only solution I can come up with is to forget about drawable selectors, and just replace the item's icon tint list and text color every time the bottom nav bar selection changes. In your navbar-hosting activity, define these two:

private val bottomNavBarStateList = arrayOf(
intArrayOf(android.R.attr.state_checked),
intArrayOf(-android.R.attr.state_checked)
)

private fun updateBottomNavBarColor(currentSelectedColor: Int) {
val colorList = intArrayOf(
ContextCompat.getColor(this, currentSelectedColor),
ContextCompat.getColor(this, R.color.text_tertiary)
)
val colorStateList = ColorStateList(bottomNavBarStateList, colorList)
bottom_navigation_bar.itemIconTintList = colorStateList
bottom_navigation_bar.itemTextColor = colorStateList
}

Then in your navbar's ItemSelectedListener, call updateBottomNavBarColor with the color you want:

bottom_navigation_bar.setOnNavigationItemSelectedListener {
when(it.itemId) {
R.id.bottom_nav_action_treatment -> {
updateBottomNavBarColor(R.color.treatment)
}
R.id.bottom_nav_action_profile -> {
updateBottomNavBarColor(R.color.profile)
}
else -> {

}
}
}

Selected tab's color in Bottom Navigation View

While making a selector, always keep the default state at the end, otherwise only default state would be used. You need to reorder the items in your selector as:

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

And the state to be used with BottomNavigationBar is state_checked not state_selected.



Related Topics



Leave a reply



Submit