Last active
May 28, 2020 20:00
-
-
Save nikartx/7a16a77ed726797cff72465b4a439f91 to your computer and use it in GitHub Desktop.
Set color for the item in BottomNavigationView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void onClickBnvMenu() { | |
bnvMenu.setOnNavigationItemSelectedListener(item -> { | |
final int itemId = item.getItemId(); | |
if (selectedCategoryId == itemId) { | |
return false; | |
} | |
switch (itemId) { | |
case R.id.item_feed: | |
// Do something | |
break; | |
case R.id.item_profile: | |
// Do something | |
break; | |
default: | |
return false; | |
} | |
selectedCategoryId = itemId; | |
setMenuItemsColor(selectedCategoryId); | |
return true; | |
}); | |
} | |
private void setMenuItemsColor(int itemId) { | |
int uncheckedColor = getResources().getColor(R.color.colorSecondaryDark); | |
int checkedColor = getResources().getColor(R.color.colorPrimary); | |
int itemColor = getResources().getColor(R.color.colorRed); | |
if (itemId == R.id.item_menu) { | |
setItemColor(itemColor, uncheckedColor); | |
} else { | |
setItemColor(checkedColor, uncheckedColor); | |
} | |
} | |
private void setItemColor(int checkedColor, int uncheckedColor) { | |
ColorStateList iconsColorStates = new ColorStateList( | |
new int[][]{ | |
new int[]{-android.R.attr.state_checked}, | |
new int[]{android.R.attr.state_checked} | |
}, | |
new int[]{ | |
uncheckedColor, | |
checkedColor | |
}); | |
ColorStateList textColorStates = new ColorStateList( | |
new int[][]{ | |
new int[]{-android.R.attr.state_checked}, | |
new int[]{android.R.attr.state_checked} | |
}, | |
new int[]{ | |
uncheckedColor, | |
checkedColor | |
}); | |
bnvMenu.setItemIconTintList(iconsColorStates); | |
bnvMenu.setItemTextColor(textColorStates); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment