I want know how can I change Bottom Navigation View's Icons when user basically selects it and then again replace it with previous icon if user selects different option.
Below is my switch case snippet.
switch (menuItem.getItemId()) {
case R.id.ic_home:
selectedFragment = new HomeFragment();
//menuItem.setIcon(R.drawable.like_colored);
break;
case R.id.ic_connect:
selectedFragment = new ConnectionFragment();
break;
case R.id.ic_add:
selectedFragment = new AddPostFragment();
break;
case R.id.ic_noti:
selectedFragment = new NotificationFragment();
break;
case R.id.ic_profile:
selectedFragment = new ProfileFragment();
break;
This will change the icon and text color of a single menu item from anywhere (e.g. onResume). The code below works fine on 4.4.2 through (at least) Pie. It's bits and pieces from here and other similar threads. Some notes:
static public void setMenuItemProperties(AppCompatActivity activity,
MenuItem item,
int resIconDrawable, int resColor) {
int id = item.getItemId();
BottomNavigationItemView m = activity.findViewById(id);
TextView t1 = m.findViewById(R.id.smallLabel);
TextView t2 = m.findViewById(R.id.largeLabel);
t1.setTextColor(activity.getResources().getColor(resColor));
t2.setTextColor(activity.getResources().getColor(resColor));
Drawable d = VectorDrawableCompat.create(activity.getResources(), resIconDrawable, null);
//Drawable d = activity.getResources().getDrawable(resIconDrawable);
item.setIcon(d);
}
Call like this (from an Activity) to select between two icons and text colors for menu item 3. (navigation
is the BottomNavigationView.)
setMenuItemProperties(this, navigation.getMenu().getItem(3),
enabled ? R.drawable.ic_settings_red_24dp : R.drawable.ic_settings_redish_24dp,
enabled ? android.R.color.white : R.color.medium_dark_grey);