Placeholder focus issue in xamarin.forms

Hello,
I'm using editor control in my app, and to set a placeholder in it i'm using following code to show /hide place holder on focused and unfocused event.

Focused += (object sender, FocusEventArgs e) => {
if(Text == Placeholder)
{
Text = "";
}
};

Unfocused += (object sender, FocusEventArgs e) => {
MyEditor obj = (MyEditor) sender;
if(string.IsNullOrEmpty(Text) || string.IsNullOrWhiteSpace(Text))
{
obj.Text = Placeholder;
}
};

Now the problem here is, in android devices user have to click 2 times on editor to open keyboard.

And if I remove the Text = "" statement then it's working properly.

Can any one tell me how can I solve this issue.
};

Answers

  • jariwalahetaljariwalahetal USMember ✭✭

    Solved it myself my adding following code in Android Editor renderer

    protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
    {
    MyEditor obj = (MyEditor)sender;
    if (e.PropertyName == "IsFocused") {
    if (obj.IsFocused) {
    if (obj.Text == obj.Placeholder) {
    ThreadPool.QueueUserWorkItem(s =>
    {
    Thread.Sleep(100); // For some reason, a short delay is required here. ((Android.Views.InputMethods.InputMethodManager)Context.GetSystemService((Android.Content.Context.InputMethodService))).ShowSoftInput(this.Control,Android.Views.InputMethods.ShowFlags.Implicit);
    });
    }
    }
    }
    base.OnElementPropertyChanged(sender, e);
    }

Sign In or Register to comment.