forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterialFormsEditTextManager.cs
74 lines (64 loc) · 2.42 KB
/
MaterialFormsEditTextManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#if __ANDROID_28__
using System;
using Android.Content;
using Android.Support.Design.Widget;
using Xamarin.Forms.Platform.Android;
namespace Xamarin.Forms.Material.Android
{
internal static class MaterialFormsEditTextManager
{
// These paddings are a hack to center the hint
// once this issue is resolved we can get rid of these paddings
// https://github.com/material-components/material-components-android/issues/120
// https://stackoverflow.com/questions/50487871/how-to-make-the-hint-text-of-textinputlayout-vertically-center
static Thickness _centeredText = new Thickness(16, 8, 12, 27);
static Thickness _alignedWithUnderlineText = new Thickness(16, 20, 12, 16);
public static void Init(TextInputEditText textInputEditText)
{
textInputEditText.TextChanged += OnTextChanged;
textInputEditText.FocusChange += OnFocusChanged;
}
public static void Dispose(TextInputEditText textInputEditText)
{
textInputEditText.TextChanged -= OnTextChanged;
textInputEditText.FocusChange -= OnFocusChanged;
}
private static void OnFocusChanged(object sender, global::Android.Views.View.FocusChangeEventArgs e)
{
if (sender is TextInputEditText textInputEditText)
{
// Delay padding update until after the keyboard has showed up otherwise updating the padding
// stops the keyboard from showing up
// TODO closure
if (e.HasFocus)
Device.BeginInvokeOnMainThread(() => UpdatePadding(textInputEditText));
else
UpdatePadding(textInputEditText);
}
}
private static void OnTextChanged(object sender, global::Android.Text.TextChangedEventArgs e)
{
if (e.BeforeCount == 0 || e.AfterCount == 0)
UpdatePadding(sender as TextInputEditText);
}
static void UpdatePadding(TextInputEditText textInputEditText)
{
Thickness rect = _centeredText;
if (!String.IsNullOrWhiteSpace(textInputEditText.Text) || textInputEditText.HasFocus)
{
rect = _alignedWithUnderlineText;
}
Context Context = textInputEditText.Context;
var left = (int)Context.ToPixels(rect.Left);
var top = (int)Context.ToPixels(rect.Top);
var right = (int)Context.ToPixels(rect.Right);
var bottom = (int)Context.ToPixels(rect.Bottom);
if(textInputEditText.PaddingLeft != left ||
textInputEditText.PaddingTop != top ||
textInputEditText.PaddingRight != right ||
textInputEditText.PaddingBottom != bottom)
textInputEditText.SetPadding(left, top, right, bottom);
}
}
}
#endif