Ganesh Gebhard
Ganesh Gebhard

Reputation: 549

Not able to use decimals with Numeric Keyboard (Xamarin Forms on Android)

I've tried several solutions given on several forums and also asked on the Xamarin Forum, but all without a result: I want to create an Entry in Xamarin Forms where the user can add a decimal number. Problem is that my numeric keyboard disables the comma.

I was able to add a dot using a custom renderer, but since I live in The Netherlands I also need to have the option of using a comma. I test on a Samsung Galaxy S10 and it doesn't work there for no visible reason.

I created a demo for you take a look at, but since I cannot upload any files here you can find it on the thread I created on the Xamarin Forum (demo file). Maybe someone here can figure out what I need to do..

Thanks in advance.

Upvotes: 2

Views: 6948

Answers (2)

Tobe
Tobe

Reputation: 198

Try any other manufactorer and Jarvan Zhangs answer will work. But the Samsung numeric keyboard always shows a dot, there's no way to change that. There are hundreds of people on the internet that want the same, not only for Xamarin but also for native android (e.g. Samsung Galaxy Note II - Comma as Numeric Keyboard Decimal Separator). I ended up replacing every dot with a comma in the TextChanged event (in Xamarin.Forms code)...

Text = Text.Replace(".", ",")

If the Samsung numeric keyboard shows a minus above the dot, that's because it is signed. You can get rid of that with a custom renderer:

if (Android.OS.Build.Manufacturer.Equals("Samsung", StringComparison.CurrentCultureIgnoreCase))
        {
            Control.InputType = InputTypes.ClassNumber | InputTypes.NumberFlagDecimal;
        }
        else
        {
            Control.KeyListener = DigitsKeyListener.GetInstance("1234567890,");
        }

The only other option is to install another keyboard, see linked SO question above...

Edit: while trying to find good examples of other failed attempts (lol) I found this acceppted answer: https://forums.xamarin.com/discussion/175286/samsung-numeric-keyboard-makes-me-cry I don't have a Samsung device in my homeoffice, so can't test, but maybe give it a shot.

Upvotes: 3

Jarvan Zhang
Jarvan Zhang

Reputation: 1013

To use the comma key in Xamarin.Forms for Android, try the following code.

public class EntryNumericKeyboardRenderer : EntryRenderer
{
    public EntryNumericKeyboardRenderer(Context context) : base(context)
    {
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            this.Control.KeyListener = DigitsKeyListener.GetInstance("1234567890,.");
        }
    }
}

Upvotes: 3

Related Questions