A NumericEditText for Xamarin.Android that accepts decimal numbers that work with any culture
It will automatically get the phone's language using CultureInfo.CurrentCulture
to figure out which characters to use as CurrencyGroupSeparator
and NumberDecimalSeparator
and automatically add them as you type.
NuGet package available:
PM> Install-Package NumericEditText-Xamarin.Android
Add the res-auto
namespace:
xmlns:num="http://schemas.android.comas/apk/res-auto"
Then you can just use the component like so:
<br.com.akamud.NumericEditText
android:id="@+id/txtNumeric"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|numberDecimal" />
To get the number typed without mask you can use the method GetNumericValue()
. If the input is invalid it will return a double.NaN
.
double number = txtNumeric.GetNumericValue();
If you want it to return 0
when the input is invalid (like empty) you can use GetNumericValueOrDefault()
:
double number = txtNumeric.GetNumericValueOrDefault();
By default it uses 2 decimal digits and (virtually) infinite number digits, but you can change it to whatever you need using two attributes:
Attribute | Description | Default Value |
---|---|---|
maxDigitsBeforeDecimal | Sets the maximum number of digits before the decimal point | 0 (infinite) |
maxDigitsAfterDecimal | Sets the maximum number of digits after the decimal point | 2 |
<br.com.akamud.NumericEditText
android:id="@+id/txtNumeric"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number|numberDecimal"
num:maxDigitsBeforeDecimal="6"
num:maxDigitsAfterDecimal="4" />
You can also change it programmatically
txtNumeric.MaxDigitsBeforeDecimal = 6;
txtNumeric.MaxDigitsAfterDecimal = 4;
NumericEditText
fires two events:
NumericValueChanged
when the value typed is changed
NumericValueCleared
when the input is cleared
You can use them like a regular event:
txtNumeric.NumericValueCleared += (object sender, NumericValueClearedEventArgs e) => {
// Value cleared
};
txtNumeric.NumericValueChanged += (object sender, NumericValueChangedEventArgs e) => {
double newValue = e.NewValue;
// New value
};
Using en-US
culture:
Input:
100,000.00
Output:
// double
100000.00
Using pt-BR
culture:
Input:
100.000,00
Output:
// double
100000.00
The original Android EditText has this annoying bug when used with inputType="number|numberDecimal"
, it won't work for different cultures that use different decimal separators (like pt-BR's ,
(comma)), so you can't have it accept 105,60
as a valid number.
This project is based on two other projects:
Android-NumberEditText by @hyperax
numeric-edittext by @hidroh
They were adapted to fit my goals, it is a bit hacky but I think someone else might find it useful.