Samsung numeric keyboard makes me cry

igorkr_10igorkr_10 Member ✭✭✭
edited December 3 in Xamarin.Forms

Hi! I have a big count of numeric entries and use custom renderer.

private void SetIsDecimal()
        {
            if (this.Control == null) return;
            if ((this.Element as RoundedEntry).IsDecimal)
            {
                Control.InputType |= InputTypes.NumberFlagDecimal;
                Console.WriteLine(CultureInfo.CurrentCulture.ToString());
                Control.KeyListener = DigitsKeyListener.GetInstance(string.Format("1234567890{0}", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
            }
        }

I use uk-ua culture.
On Pixel emulator I have two decimal separators: , . and it is OK for me.
On Samsung S8+ I have only . separator. But I need , separator for this culture.
Pixel:

For Samsung see attached file.

Main question: how can I add , separator for Samsung keyboard?
Samsung S8+ Android 9

Note: I also tried "1234567890," but have the same result.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to create a custom renderer for Entry and override the allowed characters.

    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    namespace App19F_2.Droid
    {
        class CustomEntryRenderer : EntryRenderer
        {
            public CustomEntryRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (this.Control == null) return;
    
                this.Control.KeyListener = DigitsKeyListener.GetInstance("1234567890,. -");
            }
        }
    }
    
  • igorkr_10igorkr_10 Member ✭✭✭

    @Jarvan said:
    Try to create a custom renderer for Entry and override the allowed characters.

    [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
    namespace App19F_2.Droid
    {
        class CustomEntryRenderer : EntryRenderer
        {
            public CustomEntryRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                if (this.Control == null) return;
    
                this.Control.KeyListener = DigitsKeyListener.GetInstance("1234567890,. -");
            }
        }
    }
    

    Thanks.
    But no changes. I have a keyboard with .- only. The button with , is not enabled

  • igorkr_10igorkr_10 Member ✭✭✭

    I have created Converter for XAML as workaround (I used it before for nullable double). It works as temporary solution.

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null)
                    return null;
    
                string stringValue = value as string;
                //if (string.IsNullOrEmpty(stringValue))
                //    return null;
                if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ",")
                {
                    stringValue = stringValue.Replace(".", ",");
                }
                if (double.TryParse(stringValue, NumberStyles.Number, CultureInfo.CurrentUICulture, out double result))
                {
                    if (result == 0)
                    {
                        return null;
                    }
                    return result;
                }
                return null;
            }
    
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited December 5

    A workaround about the issue, try to test on your side.

    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    
        if (Control != null)
        {
            this.Control.KeyListener = DigitsKeyListener.GetInstance(true, true);
            this.Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
        }
    }
    

    Check the link:
    https://github.com/xamarin/Xamarin.Forms/issues/1898#issuecomment-432539426
    https://forums.xamarin.com/discussion/comment/367874/#Comment_367874

  • igorkr_10igorkr_10 Member ✭✭✭

    @Jarvan said:
    A workaround about the issue, try to test on your side.

    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    
        if (Control != null)
        {
            this.Control.KeyListener = DigitsKeyListener.GetInstance(true, true);
            this.Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
        }
    }
    

    Check the link:
    https://github.com/xamarin/Xamarin.Forms/issues/1898#issuecomment-432539426
    https://forums.xamarin.com/discussion/comment/367874/#Comment_367874

    The keyboard has , separator. This is a progress. But the Entry shows . when I put , =)
    I don't know how this solution helps to other people but for me it is not working as expected.
    Thanks for help.

Sign In or Register to comment.