Last active
June 8, 2018 13:54
-
-
Save DevEddy/07fe39141f0f3852f46fe3ab9bf43f8f to your computer and use it in GitHub Desktop.
Xamarin.Android: Temp. fix for vertical text alignment of the SearchBar (Issue #2936)
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.Collections.Generic; | |
using System.Linq; | |
using Android.Content; | |
using Android.OS; | |
using Android.Views; | |
using Android.Widget; | |
using CloudMatic.Controls; | |
using CloudMatic.Droid.ControlsRenderer; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.Android; | |
using AView = Android.Views.View; | |
using AViewGroup = Android.Views.ViewGroup; | |
[assembly: ExportRenderer(typeof(SearchBar), typeof(SearchBarAlignmentFixRenderer))] | |
namespace CloudMatic.Droid.ControlsRenderer | |
{ | |
public class SearchBarAlignmentFixRenderer : SearchBarRenderer | |
{ | |
public SearchBarAlignmentFixRenderer(Context context) : base(context) | |
{ | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e) | |
{ | |
base.OnElementChanged(e); | |
if (Control == null) | |
return; | |
var editText = GetChildrenOfType<EditText>(Control).FirstOrDefault(); | |
if (editText == null) | |
return; | |
UpdateHorizontalAlignment(editText, Element.HorizontalTextAlignment, Context.HasRtlSupport(), ToVerticalGravityFlags(Xamarin.Forms.TextAlignment.Center)); | |
} | |
static void UpdateHorizontalAlignment(EditText view, Xamarin.Forms.TextAlignment alignment, bool hasRtlSupport, GravityFlags orMask = GravityFlags.NoGravity) | |
{ | |
if ((int)Build.VERSION.SdkInt < 17 || !hasRtlSupport) | |
view.Gravity = ToHorizontalGravityFlags(alignment) | orMask; | |
else | |
view.TextAlignment = ToTextAlignment(alignment); | |
} | |
IEnumerable<T> GetChildrenOfType<T>(AViewGroup self) where T : AView | |
{ | |
for (var i = 0; i < self.ChildCount; i++) | |
{ | |
AView child = self.GetChildAt(i); | |
var typedChild = child as T; | |
if (typedChild != null) | |
yield return typedChild; | |
if (child is AViewGroup) | |
{ | |
IEnumerable<T> myChildren = GetChildrenOfType<T>((child as AViewGroup)); | |
foreach (T nextChild in myChildren) | |
yield return nextChild; | |
} | |
} | |
} | |
static Android.Views.TextAlignment ToTextAlignment(Xamarin.Forms.TextAlignment alignment) | |
{ | |
switch (alignment) | |
{ | |
case Xamarin.Forms.TextAlignment.Center: | |
return Android.Views.TextAlignment.Center; | |
case Xamarin.Forms.TextAlignment.End: | |
return Android.Views.TextAlignment.ViewEnd; | |
default: | |
return Android.Views.TextAlignment.ViewStart; | |
} | |
} | |
static GravityFlags ToVerticalGravityFlags(Xamarin.Forms.TextAlignment alignment) | |
{ | |
switch (alignment) | |
{ | |
case Xamarin.Forms.TextAlignment.Start: | |
return GravityFlags.Top; | |
case Xamarin.Forms.TextAlignment.End: | |
return GravityFlags.Bottom; | |
default: | |
return GravityFlags.CenterVertical; | |
} | |
} | |
static GravityFlags ToHorizontalGravityFlags(Xamarin.Forms.TextAlignment alignment) | |
{ | |
switch (alignment) | |
{ | |
case Xamarin.Forms.TextAlignment.Center: | |
return GravityFlags.CenterHorizontal; | |
case Xamarin.Forms.TextAlignment.End: | |
return GravityFlags.End; | |
default: | |
return GravityFlags.Start; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment