Getting Back The Old Bottombar
Just started a new app and when including a BottomNavigationView the result look like that : I really don't get where it come from and how can I get back to the old version that a
Solution 1:
by default BottomNavigationView add shiftingmode = true when its more than 3 items. so try this
staticvoidremoveShiftMode(BottomNavigationView view) {
BottomNavigationMenuViewmenuView= (BottomNavigationMenuView) view.getChildAt(0);
try {
FieldshiftingMode= menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (inti=0; i < menuView.getChildCount(); i++) {
BottomNavigationItemViewitem= (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 ", "Unable to get shift mode field"+e);
} catch (IllegalAccessException e) {
Log.e("ERROR", "Unable to change value of shift mode"+e);
}
}
BottomNavigationViewbottomNavigationView= (BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
Solution 2:
You need to create helper class for this :
publicclassBottomNavigationViewHelper {
publicstaticvoiddisableShiftMode(BottomNavigationView view) {
BottomNavigationMenuViewmenuView= (BottomNavigationMenuView) view.getChildAt(0);
try {
FieldshiftingMode= menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (inti=0; i < menuView.getChildCount(); i++) {
BottomNavigationItemViewitem= (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);
}
}
}
In activity :
BottomNavigationViewbnav= (BottomNavigationView) findViewById(R.id.bottomnav);
BottomNavigationViewHelper.disableShiftMode(bnav);
Post a Comment for "Getting Back The Old Bottombar"