I am programmatically entering text into a bunch of Entry controls in a ContentPage. I don't want the keyboard to show since the user isn't supposed to type into them - the text comes from a different async service. But I do want the blinking cursor to show as visual feedback.
This is a common problem in Android land with everyone stating the same solutions (here and here).
So I have a custom renderer for the Entry control, which looks like this for Android:
[assembly: ExportRenderer (typeof (MyEntry), typeof (MyEntryRenderer_Android))]
public class MyEntryRenderer_Android: EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (e.OldElement == null) {
// **** this doesn't work
InputMethodManager imm = (InputMethodManager)Context.GetSystemService(global::Android.Content.Context.InputMethodService);
imm.HideSoftInputFromWindow(Control.WindowToken, HideSoftInputFlags.None);
}
}
protected override void OnAttachedToWindow()
{
// **** this doesn't work either
InputMethodManager imm = (InputMethodManager)Context.GetSystemService(global::Android.Content.Context.InputMethodService);
imm.HideSoftInputFromWindow(Control.WindowToken, HideSoftInputFlags.None);
base.OnAttachedToWindow();
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
// **** this does work
Control.RequestFocus();
if (UIControl.Instance.VoiceInputEnabled)
{
return true;
}
else
{
return false;
}
}
}
You can see my attempts at preventing the keyboard in OnElementChanged
and OnAttachedToWindow
. They don't work, it still pops up when I programmatically set focus to the control. However, if the user clicks on the entry, that OnInterceptTouchEvent override is called and that does prevent the keyboard from showing.
BTW I have also tried this in my MainActivity.cs for Android:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
//...
}
Still no dice. How can prevent it from showing?