Home > OS >  ActionBar in Android overrides the BottomNavigation
ActionBar in Android overrides the BottomNavigation

Time:10-05

When trying to change an icon to a downloaded drawable (or actually changing to any other icon during runtime), the icon changes once on the ActionBar.

I actually want to remove the ActionBar and leave only the bottomnav (tabs) for navigation, yet whatever i'm doing the icon changes only on the ActionBar.

The item inside bottom_nav_menu.xml:

    <item
    android:id="@ id/navigation_notifications"
    android:icon="@drawable/ic_dashboard_black_24dp"
    android:title="@string/title_notifications"
    app:showAsAction="ifRoom"/>

The code that changes the icon:

@Override
public boolean onPrepareOptionsMenu (Menu menu){
    menu.clear();
    getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
    menu.getItem(2).setIcon(this.bitmap_pic);

    Log.e(TAG, "Icon Changed");
    return super.onPrepareOptionsMenu(menu);
}

The result - Icon stays blank on BottomNav but appears on the ActionBar.

enter image description here

Expected result: BottomNav icon will be the image that shown on the top right.

Thanks

EDIT!

Issue was fixed after inflating the main_activity layout that contains the BottomNavView

Now the problem the picture isn't showing properly, attached a screenshot (Image is grey instead of showing the icon like in the ActionBar in the first picture):

enter image description here

Edit 2

Icon is still grey instead of showing the bitmap picture. Added:

MenuItemCompat.setIconTintMode(bottomNavigationView.getMenu().getItem(2), PorterDuff.Mode.CLEAR);

But it still shows up like in the picture below

enter image description here

Edit 3

Fixed the issue using:

        bottomNavigationView.setItemIconTintList(null);

CodePudding user response:

I'm not sure if onPrepareOptionsMenu invoked for bottom navigation bar.

You should have to update navigation menu icon from onCreate method of that Activity.

Refer below code,

val menu = navigation.menu
val menuItem = menu.findItem(R.id.navigation_notifications) // find particular menu-item using its ID.
menuItem?.icon = this.bitmap_pic

Solution for Gray icon tint, add below line.

MenuItemCompat.setIconTintMode(menuItem, PorterDuff.Mode.DST)
  • Related