Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace FloatingEntryApp.Droid.Renderers
- {
- public class MyEntryRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<FloatingEntry, TextInputLayout>
- {
- private EditText _defaultEditTextForValues;
- private bool _preventTextLoop;
- private EditText EditText => Control.EditText;
- protected override void OnElementChanged(ElementChangedEventArgs<FloatingEntry> e)
- {
- base.OnElementChanged(e);
- if (e.OldElement != null)
- {
- EditText.FocusChange -= ControlOnFocusChange;
- Control.EditText.KeyPress -= EditTextOnKeyPress;
- Control.EditText.TextChanged -= EditTextOnTextChanged;
- }
- if (e.NewElement != null)
- {
- var ctrl = CreateNativeControl();
- SetNativeControl(ctrl);
- _defaultEditTextForValues = new EditText(Context);
- Focusable = true;
- Control.HintEnabled = true;
- Control.HintAnimationEnabled = true;
- EditText.ShowSoftInputOnFocus = true;
- EditText.FocusChange += ControlOnFocusChange;
- EditText.ImeOptions = ImeAction.Done;
- SetText();
- SetHintText();
- SetTextColor();
- SetBackgroundColor();
- SetHintColor();
- SetIsPassword();
- SetKeyboard();
- SetInputType();
- var nativeEditText = Control.EditText;
- nativeEditText.SetTextSize(ComplexUnitType.Pt, 9);
- Control.EditText.TextChanged += EditTextOnTextChanged;
- Control.EditText.KeyPress += EditTextOnKeyPress;
- }
- }
- private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
- {
- if (args.HasFocus)
- {
- var manager = (InputMethodManager)Application.Context.GetSystemService(Context.InputMethodService);
- EditText.PostDelayed(() =>
- {
- EditText.RequestFocus();
- manager.ShowSoftInput(EditText, 0);
- },
- 100);
- }
- var isFocusedPropertyKey = Element.GetInternalField<BindablePropertyKey>("IsFocusedPropertyKey");
- ((IElementController)Element).SetValueFromRenderer(isFocusedPropertyKey, args.HasFocus);
- }
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- base.OnElementPropertyChanged(sender, e);
- if (e.PropertyName == FloatingEntry.PlaceholderProperty.PropertyName)
- {
- SetHintText();
- }
- if (e.PropertyName == FloatingEntry.TextColorProperty.PropertyName)
- {
- SetTextColor();
- }
- if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
- {
- SetBackgroundColor();
- }
- if (e.PropertyName == FloatingEntry.IsPasswordProperty.PropertyName)
- {
- SetIsPassword();
- }
- if (e.PropertyName == FloatingEntry.TextProperty.PropertyName)
- {
- SetText();
- }
- if (e.PropertyName == FloatingEntry.PlaceholderColorProperty.PropertyName)
- {
- SetHintColor();
- }
- if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
- {
- SetKeyboard();
- }
- if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
- {
- SetInputType();
- }
- }
- private void ElementOnHideKeyboard(object sender, EventArgs eventArgs)
- {
- var manager = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
- manager.HideSoftInputFromWindow(Control.EditText.WindowToken, 0);
- }
- private void SetIsPassword()
- {
- Control.EditText.InputType = Element.IsPassword
- ? InputTypes.TextVariationPassword | InputTypes.ClassText
- : Control.EditText.InputType;
- }
- private void SetBackgroundColor()
- {
- Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
- }
- private void SetHintText()
- {
- Control.Hint = Element.Placeholder;
- }
- private void SetHintColor()
- {
- if (Element.PlaceholderColor == Color.Default)
- {
- Control.EditText.SetHintTextColor(_defaultEditTextForValues.HintTextColors);
- }
- else
- {
- Control.EditText.SetHintTextColor(Element.PlaceholderColor.ToAndroid());
- }
- }
- private void SetTextColor()
- {
- if (Element.TextColor == Color.Default)
- {
- Control.EditText.SetTextColor(_defaultEditTextForValues.TextColors);
- }
- else
- {
- Control.EditText.SetTextColor(Element.TextColor.ToAndroid());
- }
- }
- private void SetKeyboard()
- {
- Control.EditText.InputType = Element.Keyboard.ToNative();
- }
- protected override TextInputLayout CreateNativeControl()
- {
- var layout = (TextInputLayout)LayoutInflater.From(Context).Inflate(Resource.Layout.TextInputLayout, null);
- var inner = layout.FindViewById(Resource.Id.textInputEdit);
- if (!string.IsNullOrWhiteSpace(Element.AutomationId))
- {
- inner.ContentDescription = Element.AutomationId;
- }
- return layout;
- }
- private void EditTextOnKeyPress(object sender, KeyEventArgs args)
- {
- args.Handled = args.KeyCode == Keycode.Enter;
- if (args.KeyCode == Keycode.Enter && args.Event.Action == KeyEventActions.Up)
- {
- Control.ClearFocus();
- HideKeyboard();
- ((IEntryController)Element).SendCompleted();
- }
- }
- private void EditTextOnTextChanged(object sender, Android.Text.TextChangedEventArgs args)
- {
- var selection = Control.EditText.SelectionStart;
- if (!_preventTextLoop)
- {
- Element.Text = args.Text.ToString();
- }
- if (Element == null || Element.Text == null) return;
- var index = selection > Element.Text.Length ? Element.Text.Length : selection;
- Control.EditText.SetSelection(index);
- }
- private void SetText()
- {
- _preventTextLoop = true;
- if (Control.EditText.Text != Element.Text)
- {
- Control.EditText.Text = Element.Text;
- if (EditText.IsFocused)
- EditText.SetSelection(EditText.Text.Length);
- }
- _preventTextLoop = false;
- }
- protected override void Dispose(bool disposing)
- {
- Control.EditText.KeyPress -= EditTextOnKeyPress;
- Control.EditText.TextChanged -= EditTextOnTextChanged;
- base.Dispose(disposing);
- }
- protected void HideKeyboard()
- {
- var manager = (InputMethodManager)Application.Context.GetSystemService(Context.InputMethodService);
- manager.HideSoftInputFromWindow(EditText.WindowToken, 0);
- }
- private void SetInputType()
- {
- EditText.InputType = Element.Keyboard.ToInputType();
- if (Element.IsPassword && ((EditText.InputType & InputTypes.ClassText) == InputTypes.ClassText))
- {
- EditText.InputType = EditText.InputType | InputTypes.TextVariationPassword;
- }
- if (Element.IsPassword && ((EditText.InputType & InputTypes.ClassNumber) == InputTypes.ClassNumber))
- {
- EditText.InputType = EditText.InputType | InputTypes.NumberVariationPassword;
- }
- }
- }
- }
- public class LoginPage : ContentPage
- {
- FloatingEntry EntryFour, EntryFive, EntryOne, EntryTwo, EntryThree;
- Button button;
- StackLayout detailStack;
- public LoginPage()
- {
- button = new Button
- {
- Text = "Button",
- FontSize = 20,
- FontAttributes = FontAttributes.Bold,
- TextColor = Color.RoyalBlue,
- BackgroundColor = Color.White,
- HeightRequest = 50,
- };
- StackLayout mainStack = new StackLayout();
- mainStack.BackgroundColor = Color.White;
- mainStack.Spacing = 5;
- StackLayout labelStack = new StackLayout();
- labelStack.BackgroundColor = Color.Gray;
- labelStack.HeightRequest = 100;
- Label loginHeaderLabel = new Label();
- loginHeaderLabel.Text = "Heading";
- loginHeaderLabel.FontSize = 25;
- loginHeaderLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
- loginHeaderLabel.VerticalOptions = LayoutOptions.EndAndExpand;
- loginHeaderLabel.HorizontalTextAlignment = TextAlignment.Start;
- loginHeaderLabel.VerticalTextAlignment = TextAlignment.End;
- loginHeaderLabel.TextColor = Color.White;
- loginHeaderLabel.FontAttributes = FontAttributes.Bold;
- loginHeaderLabel.Margin = new Thickness(30, 5, 5, 10);
- labelStack.Children.Add(loginHeaderLabel);
- ScrollView scrollView = new ScrollView();
- scrollView.BackgroundColor = Color.FromHex("#FFFFFF");
- detailStack = new StackLayout();
- detailStack.Padding = new Thickness(20, 10, 20, 20);
- detailStack.Spacing = 5;
- EntryOne = new FloatingEntry();
- EntryOne.Placeholder = "Entry One";
- EntryOne.PlaceholderColor = Color.Green;
- EntryOne.TextColor = Color.Black;
- EntryOne.HorizontalTextAlignment = TextAlignment.Start;
- EntryOne.Keyboard = Keyboard.Url;
- EntryTwo = new FloatingEntry();
- EntryTwo.Placeholder = "Entry Two";
- EntryTwo.PlaceholderColor = Color.Green;
- EntryTwo.TextColor = Color.Black;
- EntryThree = new FloatingEntry();
- EntryThree.Placeholder = "Entry Three";
- EntryThree.PlaceholderColor = Color.Green;
- EntryThree.TextColor = Color.Black;
- EntryThree.HorizontalTextAlignment = TextAlignment.Start;
- EntryFour = new FloatingEntry();
- EntryFour.Placeholder = "Entry Four";
- EntryFour.PlaceholderColor = Color.Green;
- EntryFour.TextColor = Color.Black;
- EntryFour.HorizontalTextAlignment = TextAlignment.Start;
- EntryFive = new FloatingEntry();
- EntryFive.Placeholder = "Entry Five";
- EntryFive.PlaceholderColor = Color.Green;
- EntryFive.TextColor = Color.Black;
- EntryFive.HorizontalTextAlignment = TextAlignment.Start;
- EntryFive.IsPassword = true;
- detailStack.Children.Add(EntryOne);
- detailStack.Children.Add(EntryTwo);
- detailStack.Children.Add(EntryThree);
- detailStack.Children.Add(EntryFour);
- detailStack.Children.Add(EntryFive);
- detailStack.Children.Add(button);
- scrollView.Content = detailStack;
- mainStack.Children.Add(labelStack);
- mainStack.Children.Add(scrollView);
- Content = mainStack;
- }
- }
Add Comment
Please, Sign In to add comment