Skip to content

Instantly share code, notes, and snippets.

@nikartx
Last active May 28, 2020 20:00
Show Gist options
  • Save nikartx/7a16a77ed726797cff72465b4a439f91 to your computer and use it in GitHub Desktop.
Save nikartx/7a16a77ed726797cff72465b4a439f91 to your computer and use it in GitHub Desktop.
Set color for the item in BottomNavigationView
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