how to hide keyboard when entry is focused in xamarin forms

Viewed 221

Im trying to hide a soft keyboard in android device by having the entry focused by default i.e., when the page is loaded entry should be focused automatically and keyboard should be hidden

Can anyone please help me on this.

 protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        this.Control.RequestFocus();
        InputMethodManager imm = InputMethodManager.FromContext(CrossCurrentActivity.Current.Activity.ApplicationContext);
        imm.HideSoftInputFromWindow(CrossCurrentActivity.Current.Activity.Window.DecorView.WindowToken, HideSoftInputFlags.None);
    }
2 Answers

We can set the soft keyboard status as hidden. Refer Link
Since you are developing in Xamarin, Please try adding below piece of code in the OnElementChanged of the Custom Renderer
var window = ((Activity)Forms.Context).Window;
window.SetSoftInputMode(SoftInput.StateHidden);


    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
     {
                base.OnElementChanged(e);
                var window = ((Activity)Forms.Context).Window;
                window.SetSoftInputMode(SoftInput.StateHidden);
                Control.RequestFocus();
                 if ((e.OldElement == null) && (Control != null))
                {
                    var edittext = (EditText)Control;
                    edittext.ShowSoftInputOnFocus = false;
                }
     }


This will hide the Keyboard on Page Load

You could use custom renderer.

[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
namespace App2_XF.Droid
{
public class MyEntryRenderer : EntryRenderer
{
    public MyEntryRenderer(Context context) : base(context)
    {

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
           
            Control.InputType = Android.Text.InputTypes.Null;
        }
    }
}
}
Related