How to disable BottomNavigationView shift mode?
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
Remove Animation/Shifting mode from BottomNavigationView android
Ok i found a way in case it helps someone else. So by default BottomNavigationView add shiftingmode = true when its more than 3 items.
At this moment you cannot change it through existing API and the only way to disable shift mode is to use reflection.
So we can use this helper to get rid of this:
class BottomNavigationViewHelper {
static void removeShiftMode(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);
item.setShiftingMode(false);
// set once again checked value, so view will be updated
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field");
} catch (IllegalAccessException e) {
Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode");
}
}
}
and then use it like this:
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
Hope this helps someone with the same problem with me!!!
Remember, you'll need to execute this method each time you change menu items in your BottomNavigationView.
UPDATE
As per different stackoverflow question, 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;
}
Android Kotlin - Material Bottom Navigation Disable Shift Mode and Selete Menu
Answer 2. Use false, Bottom Navigation will not select the icon
bottom_navigation.setOnNavigationItemSelectedListener {
when(it.itemId) {
R.id.homeFragment -> {
showNav(HomeFragment())
true
}
R.id.accountFragment -> {
startActivity(Intent(this, MainActivity2::class.java))
false
} else -> true
}
}
Disable reselecting in BottomNavigationView
It was surprisingly simple!
NavigationUI.setupWithNavController(bottomNavigationView, hostNavController);
bottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
// Nothing here to disable reselect
}
});
How do I disable the animation in Bottom Navigation View when icons are clicked?
//Create a new class Bottom Navigation Bar helper and then implement it in the buttons classes
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
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 just use this code in every button class
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu=bottomNavigationView.getMenu();
MenuItem menuItem=menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
This will make the buttons perform nicely.
I am new to android, the code is not written by me but I hope it helps you
Can I disable shifting in specific BottomNavigationView item on specific logic?
Well the answer is quite simple, for the condition you want the transition to happen, return true
for the condition where you dont want return false
Taking your code into account, it should be
mBottomNav.setOnNavigationItemSelectedListener(
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
return true;
}
else {
showLongToast("User Not Logged In");
return false;
}
}
});
if you check the documentation for the Navigation selection listener, you can see
/**
* Listener for handling selection events on bottom navigation items.
*/
public interface OnNavigationItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* @param item The selected item
*
* @return true to display the item as the selected item and false if the item should not
* be selected. Consider setting non-selectable items as disabled preemptively to
* make them appear non-interactive.
*/
boolean onNavigationItemSelected(@NonNull MenuItem item);
}
Related Topics
How to Parse the Androidmanifest.Xml File Inside an .Apk Package
Check If Application Is on Its First Run
Can Not Find a View With Findviewbyid()
Self-Signed Ssl Acceptance on Android
Using Facebook Sdk in Android Studio
How to Prevent Screen Capture in Android
How to Get the Resource Id of an Image If I Know Its Name
Disabling the Fullscreen Editing View for Soft Keyboard Input in Landscape
Android App Out of Memory Issues - Tried Everything and Still at a Loss
Resources$Notfoundexception: Resource Is Not a Drawable (Color or Path)
Sending Message Through Whatsapp
Determine If Android App Is Being Used For the First Time
Android Studio Keeps Refusing to Resolve Com.Android.Support:Appcompat-V7:29.0.1
Clicking Urls Opens Default Browser
How to Disable an Android Button
Where Is Debug.Keystore in Android Studio
Android Edittext Delete(Backspace) Key Event
Adding Local .Aar Files to Gradle Build Using "Flatdirs" Is Not Working