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
How to Display a Pdf Via Android Web Browser Without "Downloading" First
How to Get Imsi Number in Android Using Command Line
Error With Android Studio: Cannot Launch Avd in Emulator
Http Failure Response for (Unknown Url): 0 Unknown Error on Android
Android Emulator Is Start But Window Show Blank Screen
Detect or Prevent If User Uses Fake Location
How to Set Space Between Listview Items in Android
React Native | Failed to Install the App. Please Accept All Necessary Sdk Licenses Using Sdk Manager
Remove Apk from Library in Google Play Developer Console
Android: How to Open a Specific Folder Via Intent and Show Its Content in a File Browser
Notification Disappears After Showing
Android Studio - Xml Editor Autocomplete Not Working With Support Libraries
Adb Is Not Detecting My Android Device on Ubuntu
Configure Android Edittext to Allow Decimals and Negatives
How to Make Service That Is Unremovable Even If Killed by the System
How to Show Text in Lower Case in Android
How to Install the App Twice Without Interference in Android