Programmatically hiding soft keyboard does not show it on the next tap on entry for UWP

I have an entry which is focused on appearing event and hiding soft keyboard by calling platform specific service like: InputPane.GetForCurrentView().TryHide() for UWP
and
var imm = InputMethodManager.FromContext(CrossCurrentActivity.Current.Activity.ApplicationContext);
imm.HideSoftInputFromWindow(CrossCurrentActivity.Current.Activity.Window.DecorView.WindowToken,
HideSoftInputFlags.None) for Android

The issue is the second click on the focused entry does not show the keyboard on UWP while in Android is shown as usual.
The workaround is to unfocus the entry and focus it again, that will bring the keyboard.

Is there any solution for this issue? Am i closing it wrong on UWP?
TapGesture and Click does not work on enabled and focused entry.

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'd start by going back to the cause of the problem.
    You're focusing an Entry but then forcing the keyboard down. Why? That's weird.
    I don't think strange behavior and controls not acting as expected should be a surprise when things like this are being done. There is lots of tracking taking place in the OS about focusing and showing/hiding hte keyboard etc. Then you go in an muck about. It not surprising the OS gets confused about the state and what it should do next.

    Back to the source - why are you doing that?

  • NicolaePopovNicolaePopov USMember ✭✭

    On devices with scanner functionality implemented by manufacturer, the scanned data will fill the focused input. So in order to have this, i need just to focus the input. Focusing will open the keyboard as well, which is not the desired behavior. The user will type the data in case scanner is not working or damaged barcodes. For typing he is supposed to click on the focused input to show the keyboard. This is the case for Android, UWP instead does not open the keyboard. Nor do i have some events to fire.

  • JohnHardmanJohnHardman GBUniversity mod

    @AlmaJensen.9398 - Whilst I agree with @ClintStLaurent about the risk of putting the system into an unknown state, I wonder if you've found a shareable, pragmatic solution to this in the work you've been doing?

  • AlmaJensen.9398AlmaJensen.9398 USMember ✭✭✭

    Yes mucking about with Focus() isn't something I enjoy doing.

    The HID scanner implementation I'm working with involves and Entry that I'm trying to keep in a hidden state. I don't want to the user to muck up the barcodes that are scanned by typing in the entry field.

    iOS HID scanners are nice in a way as they usually ship with a setting that can disable the virtual keyboard.

    I only just went back to looking at Android and UWP and started running into something like what @NicolaePopov is describing. I'm interested in only using a single Entry not multiple. I'll give this some more thought and get back to you.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited August 2017

    Yuck... I hate keyboard wedges.
    But the problem is anything you do to keep the user from typing keeps the HID keyboard wedge from typing.
    All they have to do is connect a bluetooth keyboard and it won't matter what you do with the soft keyboard.

    Of course the other solution is to just stop playing with these kiddie keyboard wedge scanners. I always hated those darned things back in my Point of Sale system days. Use a 'real' scanner than brings data in a COM port (physical USB or virtual over bluetooth). That way you can get the data without having to focus a UI control. Values comes in from the scanner directly to a property, which you can bind to a Label instead of an Entry and all the issues are resolved at the start of the workflow instead of trying to bandaide them at the end.

    Or just don't use a scanner at all - use the camera on the device as the scanner.

  • NicolaePopovNicolaePopov USMember ✭✭

    @JohnHardman - I will try today and let you know. Thanks

  • NicolaePopovNicolaePopov USMember ✭✭
    edited September 2017

    Also guys, is there any event i can trigger/catch when keyboard is shown on Xamarin?
    On UWP i can listen on the OnShowing (custom service) like this:

    `[assembly: Xamarin.Forms.Dependency(typeof(KeyboardService))]
    namespace KTN.WASCOS.Mobile.UWP.Services
    {
    public class KeyboardService : IKeyboardService
    {
    public void Hide()
    {
    InputPane.GetForCurrentView().Showing += OnShowing;
    InputPane.GetForCurrentView().TryHide();
    }

        public void Show()
        {
            InputPane.GetForCurrentView().TryShow();
        }
    
        private static void OnShowing(InputPane sender, InputPaneVisibilityEventArgs args)
        {
        // do my job
        }
    }
    

    }`

    On Android i cannot find a way to go.

  • NicolaePopovNicolaePopov USMember ✭✭
    edited September 2017

    Guys, is there any way to listen to an event on keyboard show on Xamarin?
    I have implemented this on UWP like:
    InputPane.GetForCurrentView().Showing += OnShowing;
    I cannot a suitable way to make it work on Android.

    Edit:
    Found a solution here https://forums.xamarin.com/discussion/26016/how-to-avoid-showing-keyboard-when-focusing-entrycell

  • NicolaePopovNicolaePopov USMember ✭✭

    @JohnHardman - property PreventKeyboardDisplayOnProgrammaticFocus found, custom entry works great on UWP, thanks.

  • NicolaePopovNicolaePopov USMember ✭✭

    Finally implemented like this: added custom entry with Action OnHideKeyboard defined which i will call from a method HideKeyboard (this is needed for Android) ` public event Action OnHideKeyboard;

        public void HideKeyboard()
        {
            OnHideKeyboard?.Invoke();
        }`
    

    On Android:
    added custom renderer for this custom entry:
    `protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);

            if (Control == null)
                return;
    
            var keyboardControlEntry = Element as KeyboardControlEntry;
            if (keyboardControlEntry != null)
                keyboardControlEntry.OnHideKeyboard += OnHideKeyboard;
        }
    
        private static void OnHideKeyboard()
        {
            var imm = InputMethodManager.FromContext(CrossCurrentActivity.Current.Activity.ApplicationContext);
    
            imm.HideSoftInputFromWindow(CrossCurrentActivity.Current.Activity.Window.DecorView.WindowToken,
                HideSoftInputFlags.None);
        }`
    

    On UWP:
    `protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);

            if (Control == null)
                return;
    
            Control.PreventKeyboardDisplayOnProgrammaticFocus = true;
        }`
    

    In this way, i added a completed Command for my custom entry which will be called when barcodes scanned or user pushed complete button.
    Nice and clean. Thank you all for you support. Please let me know if any improvements/questions arise.

  • rio_riyario_riya INMember ✭✭

    @JohnHardman Is there any way to prevent the soft keyboard when user taps the Entry for Xamarin Forms UWP?

  • JohnHardmanJohnHardman GBUniversity mod
    edited May 2019

    @rio_riya said:
    @JohnHardman Is there any way to prevent the soft keyboard when user taps the Entry for Xamarin Forms UWP?

    If you are using a recent XF version, you could possibly use the IsReadOnly property of Entry - see https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/entry#preventing-text-entry . I haven't tried it, but give it a go.

    Alternatively, looking back at the original post on this thread from @NicolaePopov , I would assume that you could do what he was doing, i.e. InputPane.GetForCurrentView().TryHide(). That's more work though.

Sign In or Register to comment.