Created
November 18, 2018 15:10
-
-
Save davidortinau/67dfa78533df363a57568eca28e5da25 to your computer and use it in GitHub Desktop.
Displays Android bottom tabs fixed like iOS with label visible and shifting disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Support.Design.Internal; | |
using Android.Support.Design.Widget; | |
using Android.Views; | |
using Android.Widget; | |
using App55.Droid.Renderers; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
using Xamarin.Forms.Platform.Android.AppCompat; | |
#if __ANDROID81__ | |
#else | |
using ALabelVisibilityMode = Android.Support.Design.BottomNavigation.LabelVisibilityMode; | |
#endif | |
[assembly: ExportRenderer(typeof(TabbedPage), typeof(DroidTabbedPageRenderer))] | |
namespace App55.Droid.Renderers | |
{ | |
public class DroidTabbedPageRenderer : Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer | |
{ | |
public DroidTabbedPageRenderer(Context context) : base(context) | |
{ | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e) | |
{ | |
base.OnElementChanged(e); | |
var activity = (FormsAppCompatActivity)Context; | |
if (e.NewElement != null) | |
{ | |
var relativeLayout = this.GetChildAt(0) as Android.Widget.RelativeLayout; | |
if (relativeLayout != null) | |
{ | |
var bottomNarView = relativeLayout.GetChildAt(1).JavaCast<BottomNavigationView>(); | |
bottomNarView.SetShiftMode(false, false); | |
} | |
} | |
} | |
} | |
public static class BottomNavigationViewUtils | |
{ | |
public static void SetShiftMode(this BottomNavigationView bottomNavigationView, bool enableShiftMode, bool enableItemShiftMode) | |
{ | |
try | |
{ | |
using (var menuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView) | |
{ | |
if (menuView == null) | |
{ | |
System.Diagnostics.Debug.WriteLine("Unable to find BottomNavigationMenuView"); | |
return; | |
} | |
#if __ANDROID81__ | |
var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode"); | |
shiftMode.Accessible = true; | |
shiftMode.SetBoolean(menuView, enableShiftMode); | |
shiftMode.Accessible = false; | |
shiftMode.Dispose(); | |
#else | |
if (enableShiftMode) | |
bottomNavigationView.LabelVisibilityMode = ALabelVisibilityMode.LabelVisibilityAuto; | |
else | |
bottomNavigationView.LabelVisibilityMode = ALabelVisibilityMode.LabelVisibilityLabeled; | |
#endif | |
for (int i = 0; i < menuView.ChildCount; i++) | |
{ | |
var child = menuView.GetChildAt(i); | |
var item = child as BottomNavigationItemView; | |
if (item != null) | |
{ | |
#if __ANDROID81__ | |
item.SetShiftingMode(enableItemShiftMode); | |
#else | |
item.SetShifting(enableItemShiftMode); | |
#endif | |
item.SetChecked(item.ItemData.IsChecked); | |
} | |
child.Dispose(); | |
} | |
menuView.UpdateMenuView(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine($"Unable to set shift mode: {ex}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment