I recently blogged about Android’s super fancy new BottomNavigationView, and how easy it was to get started with them in your Xamarin.Android application.
I also went through a lot of color customizations that developers can blend in to really make their BottomNavigationView stand out. While this new bottom navigation is awesome, it is also still brand new with a lot of features locked behind internals and hidden fields, which is very sad. Recently I was asked about how to disable the “ShiftMode” of the icons on the bottom when more than 3 sections were added. I first had to go try it for myself to see what they were talking about and sure enough there it was:
Yup, that is no good at all. I actually remember this from the first version of the Google I/O app and they scrambled to turn it off. Luckily, this is actually programmed into the underlying control but locked up in a hidden property. Do not fear as reflection is here! I wrote small utility class to turn it off:
/// <summary> | |
/// Enable or disable shift mode on bottom navigation view | |
/// </summary> | |
/// <param name="bottomNavigationView"></param> | |
/// <param name="enabled"></param> | |
public static void SetShiftMode(this BottomNavigationView bottomNavigationView, bool enableShiftMode, bool enableItemShiftMode) | |
{ | |
try | |
{ | |
var menuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView; | |
if (menuView == null) | |
{ | |
System.Diagnostics.Debug.WriteLine("Unable to find BottomNavigationMenuView"); | |
return; | |
} | |
var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode"); | |
shiftMode.Accessible = true; | |
shiftMode.SetBoolean(menuView, enableShiftMode); | |
shiftMode.Accessible = false; | |
shiftMode.Dispose(); | |
for(int i = 0; i < menuView.ChildCount; i++) | |
{ | |
var item = menuView.GetChildAt(i) as BottomNavigationItemView; | |
if (item == null) | |
continue; | |
item.SetShiftingMode(enableItemShiftMode); | |
item.SetChecked(item.ItemData.IsChecked); | |
} | |
menuView.UpdateMenuView(); | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Unable to set shift mode: {ex}"); | |
} | |
} | |
//Called like: | |
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation); | |
bottomNavigation.SetShiftMode(false, false); |
And here you go!
I have also updated my sample on GitHub with this code in it too!