Xamarin.Forms Entry field that prevents soft keyboard from showing up on Focus

Created on 27 Nov 2018  ·  17Comments  ·  Source: xamarin/Xamarin.Forms

Description

Support to prevent soft keyboard on Entry field for Android, iOS, and UWP head projects in Xamarin.Forms.

Currently, Entry field in Xamarin.Forms provides no functionality to prevent soft keyboard from showing up entirely. There are solution out there that will hide it but they make things even worse since now you have keyboard popping up briefly, then disappearing from the screen.

It feels like having a way to disable keyboard from showing up entirely on an Entry field makes sense. For example, I make my own buttons, that resemble keyboard. Tapping on each of these, puts some characters in the Entry field without showing keyboard but only when Entry field receives focus.

Another example, scanning with external barcode scanners into a field when it has focus only. Here also makes no sense to show keyboard.

Steps to Reproduce

  1. Create simple View with Entry Field
  2. Focus on that Entry field
  3. Soft keyboard will show up

Expected Behavior

It would be nice to be able to disable keyboard from showing up entirely on an Entry field focus. We need this for all 3 platforms in Xamarin.Forms, on UWP, iOS and Android

Actual Behavior

This is currently impossible to do. There are solution that hide keyboard (using renderers or other methods) which makes it even worse since keyboard shows briefly, then hides. In my example on scanning above, this solution makes it ridiculous.

entry 2 good first issue hacktoberfest 🍻 help wanted proposal-accepted enhancement ➕ up-for-grabs

Most helpful comment

Any progress on this recommendation?

All 17 comments

When #4384 is merged it will make this possible for a custom renderer on Android.

That said, it may be worth exploring adding something like a ShowSoftKeyboardOnFocus property to InputView with the default set to true.

When #4384 is merged it will make this possible for a custom renderer on Android.

That said, it may be worth exploring adding something like a ShowSoftKeyboardOnFocus property to InputView with the default set to true.

I was asking for Xamarin.Forms, not Android. We need this for Android, iOS, and definitely UWP as that is the most problematic as it turns out. Xamarin.EnableKeyboardHelperEffect suggested by @masonyc is not supporting UWP and it not preventing but rather hiding keyboard once opened on Android and iOs which is not that great considering that you get popups that then go away.

perhaps this effect can solve your problem for the moment?https://github.com/masonyc/Xamarin.EnableKeyboardEffect

@masonyc Looks like it could, thanks. Looks like only code is required on iOS FinishedLaunching while on android there is no code, XAML itself is sufficient?
Also, why do you have comment "Write here" in FinishedLaunching?

@masonyc Looks like it could, thanks. Looks like only code is required on iOS FinishedLaunching while on android there is no code, XAML itself is sufficient?
Also, why do you have comment "Write here" in FinishedLaunching?

In iOS, we need that line to init the effect but in android, it does itself. "Write here" comment just a reminder that you don't forget that line

@masonyc Looks like it could, thanks. Looks like only code is required on iOS FinishedLaunching while on android there is no code, XAML itself is sufficient?
Also, why do you have comment "Write here" in FinishedLaunching?

In iOS, we need that line to init the effect but in android, it does itself. "Write here" comment just a reminder that you don't forget that line

Thanks I will give it a try. I'd suggest updating wiki to clarify that for others. Much appreciated.

@masonyc I created 3 new issues

When #4384 is merged it will make this possible for a custom renderer on Android.
That said, it may be worth exploring adding something like a ShowSoftKeyboardOnFocus property to InputView with the default set to true.

I was asking for Xamarin.Forms, not Android. We need this for Android, iOS, and perhaps UWP

That is a pull request against Xamarin Forms, not Xamarin Android.

When #4384 is merged it will make this possible for a custom renderer on Android.
That said, it may be worth exploring adding something like a ShowSoftKeyboardOnFocus property to InputView with the default set to true.

I was asking for Xamarin.Forms, not Android. We need this for Android, iOS, and perhaps UWP

That is a pull request against Xamarin Forms, not Xamarin Android.

True it is against Xamarin.Forms,but the reply was only for Android if you read it. My response is that we need solution for iOS, Android and UWP

@masonyc Looks like it could, thanks. Looks like only code is required on iOS FinishedLaunching while on android there is no code, XAML itself is sufficient?
Also, why do you have comment "Write here" in FinishedLaunching?

In iOS, we need that line to init the effect but in android, it does itself. "Write here" comment just a reminder that you don't forget that line

Unfortunately, this solution has 2 problems:

  1. solution is hiding keyboard, not preventing it. I created the ticket but it looks like communication with dev went cold.
  2. Another problem, no UWP support which is most important to us - this is biggest problem for us

Also, the keyboard is prevented only when Entry is rendered, meaning once you load the view. But if you click on say a popup message to close it, you then have to call Focus to focus on that field and programmatic calls to Focus will not prevent but show, then hide keyboard resulting in strange behavior where keyboard keeps showing briefly for apparent no reason.

perhaps this effect can solve your problem for the moment?https://github.com/masonyc/Xamarin.EnableKeyboardEffect

Unfortunately no. I have been using it but we have issues with it, mainly lack of UWP support.

Any progress on this recommendation?

I only this functionality on Android. I have found that using a custom renderer and setting ShowSoftInputOnFofus to false works when I tap on the entry, but if I call Entry.Focus() then it shows the keyboard. Is this a bug or expected behavior? It would be nice to have it be consistent.

@samhouts can you answer my question above? I am using a scanner device and when I call Entry.Focus I do not want the keyboard to show up.

@DanielGlick45 Exact same situation here. Did you find a solution?

@stavroaudi I did not. I am just ignoring the issue for now until it is fixed

I only this functionality on Android. I have found that using a custom renderer and setting ShowSoftInputOnFofus to false works when I tap on the entry, but if I call Entry.Focus() then it shows the keyboard. Is this a bug or expected behavior? It would be nice to have it be consistent.

I have a similar problem. The problem is that Entry.Focus () forcibly opens SoftKeyboard. My decision, when I get a new barcode, I do a defocus on the page and get it again, but with a native control that SoftKeyboard does not call. I understand the need for focus through clearing the Entry text.
On the Page, when I get a new barcode

        MessagingCenter.Subscribe<App, string>(this, "Barcode", async (sender, barcode) =>
        {
            if (!IsBusy)
            {
                quantityEntry.Unfocus();
                quantityEntry.Text = "";
                quantityEntry.Text = "1";
            }
        });

On the new renderer

public class SelectedEntryRenderer : EntryRenderer
{
    public SelectedEntryRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        //if (this.Control != null)
        //{
        //    Control.SetPadding(10, 0, 0, 15);
        //    Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagSigned | Android.Text.InputTypes.NumberFlagDecimal;
        //    Control.TextAlignment = Android.Views.TextAlignment.Center;


        //}

        if (e.NewElement != null)
        {
            ((SelectedEntry)e.NewElement).PropertyChanging += OnPropertyChanging;
            ((SelectedEntry)e.NewElement).TextChanged += SelectedEntryRenderer_TextChanged;
        }

        if (e.OldElement != null)
        {
            ((SelectedEntry)e.OldElement).PropertyChanging -= OnPropertyChanging;
            ((SelectedEntry)e.OldElement).TextChanged -= SelectedEntryRenderer_TextChanged;
        }

        this.Control.ShowSoftInputOnFocus = false;

        if (e.OldElement == null)
        {
            var nativeEditText = (global::Android.Widget.EditText)Control;
            nativeEditText.SetSelectAllOnFocus(true);
            nativeEditText.RequestFocus();
            nativeEditText.SelectAll();
        }

    }

    private void SelectedEntryRenderer_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        if (string.IsNullOrEmpty(e.OldTextValue) && !string.IsNullOrEmpty(e.NewTextValue))
        {

            var nativeEditText = (global::Android.Widget.EditText)Control;
            nativeEditText.RequestFocus();  //got a focus again without SoftKeyboard
            nativeEditText.SelectAll();
        }
    }

    private void OnPropertyChanging(object sender, Xamarin.Forms.PropertyChangingEventArgs propertyChangingEventArgs)
    {
        if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
        {
            // fully dismiss the soft keyboard 
            InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
            imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);

            var nativeEditText = (global::Android.Widget.EditText)Control;
            nativeEditText.SelectAll();
        }

    }
}
Was this page helpful?
0 / 5 - 0 ratings