Forum Xamarin.Forms

How to get numeric keyboard for Windows tablets?

judsonabrahamjudsonabraham USMember ✭✭✭
edited February 2019 in Xamarin.Forms

I want numeric keyboard for Windows tablets. I used code

Keyboard="Numeric"
but it gets numeric keyboard for Android devices, iOS devices and Windows phone. It doesn't bring numeric keyboard for Windows Tablet. I refered this link (https://stackoverflow.com/questions/46413795/how-to-change-the-keyboard-layout-of-a-uwp-app-in-xamarin-forms) it says that getting a numeric keyboard in tablet is not possible. I want to know is there any way I can get this feature. Maybe using custom renderer or dependency injection. If so how?

Best Answer

  • judsonabrahamjudsonabraham USMember ✭✭✭
    Accepted Answer

    I used this renderer code and it worked like a charm

    [assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
    namespace App10.UWP
    { 
        public class MyEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.InputScope = Element.Keyboard.ToInputScope();
                }
            }
        }
    }
    

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @judsonabraham

    My tablet battery is dead at the moment, but using a soft/touch keyboard (enabled in the Windows accessibility settings), I have tested the following on my Windows desktop, which works. I would assume that it would also work on a tablet.

    The standard Xamarin.Forms.Entry properties do not support what you want. Use a custom Entry renderer to tweak the Windows FormsTextBox control, as follows. In this code nativeTextField is the FormsTextBox.

                                InputScope inputScope = new InputScope();
                                inputScope.Names.Add(new InputScopeName
                                {
                    NameValue = InputScopeNameValue.NumericPin // For 0-9 only
                                    //NameValue = InputScopeNameValue.Number // for numerics, but allowing other characters
                                    //NameValue = InputScopeNameValue.CurrencyAmountAndSymbol // For monetary values
                                });
                                nativeTextField.InputScope = inputScope;
    
                                // I also do the following, but you may not need these bits
                                nativeTextField.AcceptsReturn = false;
                                nativeTextField.IsSpellCheckEnabled = false;
                                nativeTextField.IsTextPredictionEnabled = false;
                                nativeTextField.TextReadingOrder = TextReadingOrder.UseFlowDirection;
    
    
  • JohnHardmanJohnHardman GBUniversity admin

    @JohnHardman said:
    @judsonabraham

    My tablet battery is dead at the moment, but using a soft/touch keyboard (enabled in the Windows accessibility settings), I have tested the following on my Windows desktop, which works. I would assume that it would also work on a tablet.

    The standard Xamarin.Forms.Entry properties do not support what you want. Use a custom Entry renderer to tweak the Windows FormsTextBox control, as follows. In this code nativeTextField is the FormsTextBox.

                                InputScope inputScope = new InputScope();
                                inputScope.Names.Add(new InputScopeName
                                {
                                    NameValue = InputScopeNameValue.NumericPin // For 0-9 only
                                    //NameValue = InputScopeNameValue.Number // for numerics, but allowing other characters
                                    //NameValue = InputScopeNameValue.CurrencyAmountAndSymbol // For monetary values
                                });
                                nativeTextField.InputScope = inputScope;
    
                                // I also do the following, but you may not need these bits
                                nativeTextField.AcceptsReturn = false;
                                nativeTextField.IsSpellCheckEnabled = false;
                                nativeTextField.IsTextPredictionEnabled = false;
                                nativeTextField.TextReadingOrder = TextReadingOrder.UseFlowDirection;
    
    

    Windows being Windows, there is more than one type of soft keyboard that can appear. The above code works for the "touch keyboard, or Soft Input Panel (SIP)". However, there is a separate On-Screen Keyboard which is a separate program, and the above does not change that. That sounds like a change Microsoft needs to make in a future release of Windows. For more info, see https://docs.microsoft.com/en-us/windows/uwp/design/input/use-input-scope-to-change-the-touch-keyboard

  • judsonabrahamjudsonabraham USMember ✭✭✭
    Accepted Answer

    I used this renderer code and it worked like a charm

    [assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))]
    namespace App10.UWP
    { 
        public class MyEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.InputScope = Element.Keyboard.ToInputScope();
                }
            }
        }
    }
    
  • JohnHardmanJohnHardman GBUniversity admin

    @judsonabraham said:
    Control.InputScope = Element.Keyboard.ToInputScope();

    It depends on how fine-grained control you want over what a "numeric" keyboard looks like.

    As per https://forums.xamarin.com/discussion/comment/366351/#Comment_366351
    the following values will give you finer-grained control:

    NameValue = InputScopeNameValue.NumericPin // For 0-9 only
    NameValue = InputScopeNameValue.Number // for numerics, but allowing other characters
    NameValue = InputScopeNameValue.CurrencyAmountAndSymbol // For monetary values
    
Sign In or Register to comment.