Xamarin Forms - Hiding the Entry Soft Keyboard

Clickety-clack ⌨, no turning back

We’re all familiar with onscreen keyboards (or “soft keyboards”), which have become the primary text input method in a world of interactive glass. Hiding the soft keyboard may seem like a silly prank to play on an unsuspecting user. However, in situations where a dedicated hardware input is available you may not want the soft keyboard popping in and out as the focus changes.

It is relatively simple if all you want to do is hide the soft keyboard on both Android and iOS, but Xamarin Forms doesn’t have this out of the box. It’s possible to achieve this using a renderer or an effect, below is an implementation using the latter.

In the shared code we’ll need to add our very simple routing effect.

public class NoKeyboardEffect : RoutingEffect
{
    public NoKeyboardEffect() : base("ControlSamples.Effects.NoKeyboardEffect")
    {
    }
}

For our Android and iOS effect implementations it’s just a matter of Googling around and reading some Stack Overflow threads on how best to hide the soft keyboard on each platform. The Xamarin Forms specific bit is knowing that the Entry control is rendered as an EditText on Android and a UITextField on iOS.

[ResolutionGroupName("ControlSamples.Effects")]
[ExportEffect(typeof(NoKeyboardEffect_Droid), nameof(NoKeyboardEffect))]
public class NoKeyboardEffect_Droid : PlatformEffect
{
    protected override void OnAttached()
    {
        try
        {
            if (Control is EditText editText)
            {
                // cursor shown, but keyboard will not be displayed
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                {
                    editText.ShowSoftInputOnFocus = false;
                }
                else
                {
                    editText.SetTextIsSelectable(true);
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"{nameof(NoKeyboardEffect)} failed to attached: {ex.Message}");
        }
    }

    protected override void OnDetached()
    {
    }
}
[ResolutionGroupName("ControlSamples.Effects")]
[ExportEffect(typeof(NoKeyboardEffect_iOS), nameof(NoKeyboardEffect))]
public class NoKeyboardEffect_iOS : PlatformEffect
{
    protected override void OnAttached()
    {
        try
        {
            if (Control is UITextField textField)
            {
                // dummy view to override the soft keyboard
                textField.InputView = new UIView();
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"{nameof(NoKeyboardEffect)} failed to attached: {ex.Message}");
        }
    }

    protected override void OnDetached()
    {
    }
}

Now to add the effect to our Entry control in XAML,

<Entry>
    <Entry.Effects>
        <effects:NoKeyboardEffect />
    </Entry.Effects>
<Entry>

Or if you prefer in code,

var entry = new Entry();
entry.Effects.Add(new NoKeyboardEffect());

Just a little side note, if you’re using the Android emulator and the soft keyboard pops up when you start typing take a look at turning off ‘Show virtual keyboard’ in the Settings.

Sample code on GitHub here.

comments powered by Disqus