Skip to content

Instantly share code, notes, and snippets.

@aboutgaurav
Created November 4, 2017 17:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aboutgaurav/7bc06bde7822502e51a06f9410dfd3e2 to your computer and use it in GitHub Desktop.
Save aboutgaurav/7bc06bde7822502e51a06f9410dfd3e2 to your computer and use it in GitHub Desktop.
Disable android 'BottomNavigationView' shift mode.
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;
/**
* Helper class to get rid of "Shift Mode" from BottomNavigationView
*/
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);
}
}
}
@raja-arumugam
Copy link

Hai, i'm getting error, null pointer exception on, BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
and BottomNavigationViewHelper.disableShiftMode(bottomNavigationView2);
Help me.

@surajsahani
Copy link

Hai, i'm getting error, null pointer exception on, BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
and BottomNavigationViewHelper.disableShiftMode(bottomNavigationView2);
Help me.

used bottom nav id properly

@aboutgaurav
Copy link
Author

aboutgaurav commented Mar 25, 2021

@surajsahani

Hai, i'm getting error, null pointer exception on, BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
and BottomNavigationViewHelper.disableShiftMode(bottomNavigationView2);
Help me.

used bottom nav id properly

Sure will check this.

Could you please show me your snippet?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment