summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Cells/EntryCellView.cs
blob: 0380a31a36cd28b6c9dd6c5cd3646f0e65454766 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using Android.Content;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using Java.Lang;

namespace Xamarin.Forms.Platform.Android
{
	public sealed class EntryCellView : LinearLayout, ITextWatcher, global::Android.Views.View.IOnFocusChangeListener, TextView.IOnEditorActionListener, INativeElementView
	{
		public const double DefaultMinHeight = 55;

		readonly Cell _cell;
		readonly TextView _label;

		Color _labelTextColor;
		string _labelTextText;

		public EntryCellView(Context context, Cell cell) : base(context)
		{
			_cell = cell;
			SetMinimumWidth((int)context.ToPixels(50));
			SetMinimumHeight((int)context.ToPixels(85));
			Orientation = Orientation.Horizontal;

			var padding = (int)context.ToPixels(8);
			SetPadding((int)context.ToPixels(15), padding, padding, padding);

			_label = new TextView(context);
			_label.SetTextAppearanceCompat(context, global::Android.Resource.Attribute.TextAppearanceListItem);

			var layoutParams = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent) { Gravity = GravityFlags.CenterVertical };
			using (layoutParams)
				AddView(_label, layoutParams);

			EditText = new EntryCellEditText(context);
			EditText.AddTextChangedListener(this);
			EditText.OnFocusChangeListener = this;
			EditText.SetOnEditorActionListener(this);
			EditText.ImeOptions = ImeAction.Done;
			EditText.BackButtonPressed += OnBackButtonPressed;
			//editText.SetBackgroundDrawable (null);
			layoutParams = new LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent) { Width = 0, Weight = 1, Gravity = GravityFlags.FillHorizontal | GravityFlags.Center };
			using (layoutParams)
				AddView(EditText, layoutParams);
		}

		public Action EditingCompleted { get; set; }

		public EntryCellEditText EditText { get; }

		public Action<bool> FocusChanged { get; set; }

		public string LabelText
		{
			get { return _labelTextText; }
			set
			{
				if (_labelTextText == value)
					return;

				_labelTextText = value;
				_label.Text = value;
			}
		}

		public Action<string> TextChanged { get; set; }

		public Element Element
		{
			get { return _cell; }
		}

		bool TextView.IOnEditorActionListener.OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
		{
			if (actionId == ImeAction.Done)
			{
				OnKeyboardDoneButtonPressed(EditText, EventArgs.Empty);
				EditText.ClearFocus();
				v.HideKeyboard();
			}

			// Fire Completed and dismiss keyboard for hardware / physical keyboards
			if (actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter)
			{
				OnKeyboardDoneButtonPressed(EditText, EventArgs.Empty);
				EditText.ClearFocus();
				v.HideKeyboard();
			}

			return true;
		}

		void IOnFocusChangeListener.OnFocusChange(global::Android.Views.View view, bool hasFocus)
		{
			Action<bool> focusChanged = FocusChanged;
			if (focusChanged != null)
				focusChanged(hasFocus);
		}

		void ITextWatcher.AfterTextChanged(IEditable s)
		{
		}

		void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
		{
		}

		void ITextWatcher.OnTextChanged(ICharSequence s, int start, int before, int count)
		{
			Action<string> changed = TextChanged;
			if (changed != null)
				changed(s != null ? s.ToString() : null);
		}

		public void SetLabelTextColor(Color color, int defaultColorResourceId)
		{
			if (_labelTextColor == color)
				return;

			_labelTextColor = color;
			_label.SetTextColor(color.ToAndroid(defaultColorResourceId));
		}

		public void SetRenderHeight(double height)
		{
			SetMinimumHeight((int)Context.ToPixels(height == -1 ? DefaultMinHeight : height));
		}

		void OnBackButtonPressed(object sender, EventArgs e)
		{
			// TODO Clear focus
		}

		void OnKeyboardDoneButtonPressed(object sender, EventArgs e)
		{
			// TODO Clear focus
			Action editingCompleted = EditingCompleted;
			if (editingCompleted != null)
				editingCompleted();
		}
	}
}