I've been struggling with the Samsung Galaxy numeric keyboard decimal separator nightmare.
I have an Entry
on which I must only accept numeric values (including decimals), so in my xaml code I wrote <Entry Keyboard="Numeric"/>
and it worked just fine on the VS19 android simulator. But when I run the app on my physical device (Samsung Galaxy S10+) the comma key is disabled and the .-
key doesn't work (it types nothing).
I made some digging online and found a few solutions, but none worked for me.
SOLUTION 1
Forcing the app culture to pt-BR (because my phone system language is brazilian portuguese) by adding this code to App()
(in App.xaml.cs
):
private void SetCultureToPTBR()
{
CultureInfo br = new CultureInfo("pt-BR");
CultureInfo.DefaultThreadCurrentCulture = br;
}
But there were no changes.
SOLUTION 2 Setting input type on a custom renderer for entries:
[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace AppCoperNitro.Droid.CustomRenderers
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null)
return;
this.Control.KeyListener = DigitsKeyListener.GetInstance(true, true);
this.Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagDecimal;
}
}
}
This method changed the .-
key to a comma key, and it kinda works (it types a .), but the decimal place is ignored (if I type 2.3 the app receives it as 23).
SOLUTION 3 This solution, but the result was the same as solution 2.
I also tried combining these solutions and their variances, but nothing works the way it should. I don't even care if the entry shows a comma or a dot, I just need the decimal number to be received correctly.
We can change the ways. According to your SOLUTION 2, the entry can input the .
, so you can do something in the Entry's UnFocused event. Such as: // Use Gabic's code
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
Entry entry = sender as Entry;
if (entry.Text.Length > 0 && Android.OS.Build.Manufacturer.Equals("Samsung", StringComparison.CurrentCultureIgnoreCase))
{
string a = entry.Text.Replace('.', ',');
entry.Text = a;
}
}
And then add the event into the xaml:
<Entry Keyboard="Numeric" Unfocused="Entry_Unfocused"/>