Skip to content

A NumericEditText for Xamarin.Android that accepts decimal numbers that work with any culture

License

Notifications You must be signed in to change notification settings

akamud/NumericEditText-Xamarin.Android

Repository files navigation

NumericEditText-Xamarin.Android

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.

Installing


NuGet package available:

PM> Install-Package NumericEditText-Xamarin.Android

Using

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();

Changing precision

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;

Events

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
};

Examples

Using en-US culture:
Input:

100,000.00

Output:

// double
100000.00

Using pt-BR culture:
Input:

100.000,00

Output:

// double
100000.00

Gif examples

en-US culture:

en-US gif

pt-BR culture:

pt-BR gif

Motivation

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.

License

MIT License

About

A NumericEditText for Xamarin.Android that accepts decimal numbers that work with any culture

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages