How to disable BottomNavigationView shift mode?

Natan Rubinstein

BottomNavigationView doesn't show menu's title that are inactive.

How to show titles of all menu elements in bottomNavigationBar? The problem is that in my case shown only title of element that is clicked.

enter image description here

Przemysław Piechota. kibao

Implementation of BottomNavigationView has condition: when there is more than 3 items then use shift mode.

At this moment you cannot change it through existing API and the only way to disable shift mode is to use reflection.

You'll need helper class:

import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;
import java.lang.reflect.Field;

public class BottomNavigationViewHelper {
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}

And then apply disableShiftMode method on your BottomNavigationView, but remember if you are inflating menu view from your code, you have to execute it after inflating.

Example usage:

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

PS.

Remember, you'll need to execute this method each time you change menu items in your BottomNavigationView.

UPDATE

You also need to update proguard configuration file (e.g. proguard-rules.pro), code above uses reflection and won't work if proguard obfuscate the mShiftingMode field.

-keepclassmembers class android.support.design.internal.BottomNavigationMenuView { 
    boolean mShiftingMode; 
}

Thanks Muhammad Alfaifi for pointing this issue and providing snippet.

UPDATE 2

As Jolanda Verhoef pointed out the new Support library (28.0.0-alpha1) and also the new Material Components library (1.0.0-beta01) offers a public property which can be used to manipulate the shifting mode over 3 menu items.

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    app:labelVisibilityMode="labeled"
    ... 
/>

In Material Components library it also applies if there are 5 menu items.

UPDATE 3

As @ThomasSunderland also pointed out, you can set this property to false app:itemHorizontalTranslation="false" without Enabled postfix to disable shifting animation.

you can check the full guide to styling the BottomNavigation here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to disable landscape mode?

From Dev

Byobu: Disable Shift+Arrow keys when in zoomed mode

From Dev

How to disable Ctrl + Shift + C shortcut in Firefox?

From Dev

How to disable the shift+anykey combinations in angularjs

From Dev

How to disable Ctrl+Shift+U?

From Dev

How to disable Ctrl+Shift+C in Firefox

From Dev

How to disable recovery mode/single user mode?

From Java

How to Disable landscape mode in Android?

From Dev

How to disable codeigniter debug mode

From Dev

How to disable WAL journal mode

From Dev

how to disable the SwipeRefreshLayout in Landscape mode

From Dev

How to disable Chrome kiosk mode?

From Dev

how to disable the SwipeRefreshLayout in Landscape mode

From Dev

How to disable HTML mode in Emacs?

From Dev

How to disable TextBox MenuFlyout when it is in edit mode

From Dev

How to enable and disable vibration mode programmatically

From Java

How to disable VS Code minimap in Windowed mode?

From Dev

org mode - how to disable some keybindings?

From Dev

How to disable Chrome's Incognito Mode?

From Dev

How to disable sleep mode of Apple watch programmatically

From Dev

react native how to disable android dev mode

From Dev

Django + Pythonanywhere: How to disable Debug Mode

From Dev

How to disable the leader key in vim insert mode

From Dev

How to disable Google Chrome Dark Mode?

From Dev

How to disable Protected Mode in Internet Explorer 9?

From Dev

How to disable Unity keyboard shortcut for Spread mode

From Dev

How to disable read only mode in MicroStrategy 9.3

From Dev

How to disable systemd service in rescue mode

From Dev

org mode - how to disable some keybindings?

Related Related

HotTag

Archive