Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akamud/numericedittext-xamarin.android

A NumericEditText for Xamarin.Android that accepts decimal numbers that work with any culture
https://github.com/akamud/numericedittext-xamarin.android

xamarin xamarin-android xamarin-components xamarin-plugin

Last synced: 11 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# NumericEditText-Xamarin.Android
![](https://raw.githubusercontent.com/akamud/NumericEditText-Xamarin.Android/master/art/Icon.png)

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
![](https://img.shields.io/nuget/v/NumericEditText-Xamarin.Android.svg?style=flat)
[NuGet package](https://www.nuget.org/packages/NumericEditText-Xamarin.Android/) available:
```
PM> Install-Package NumericEditText-Xamarin.Android
```

## Using
Add the `res-auto` namespace:
```XML
xmlns:num="http://schemas.android.comas/apk/res-auto"
```
Then you can just use the component like so:
```XML

```

To get the number typed without mask you can use the method `GetNumericValue()`. If the input is invalid it will return a `double.NaN`.

```C#
double number = txtNumeric.GetNumericValue();
```

If you want it to return `0` when the input is invalid (like empty) you can use `GetNumericValueOrDefault()`:

```C#
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

```XML

```

You can also change it programmatically
```C#
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:

```C#
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](https://raw.githubusercontent.com/akamud/NumericEditText-Xamarin.Android/master/enus-sample.gif)

### pt-BR culture:
![pt-BR gif](https://raw.githubusercontent.com/akamud/NumericEditText-Xamarin.Android/master/ptbr-sample.gif)

## Motivation
The original Android EditText has [this annoying bug](https://code.google.com/p/android/issues/detail?id=2626) 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](https://github.com/hyperax/Android-NumberEditText) by [@hyperax](https://github.com/hyperax)
[numeric-edittext](https://github.com/hidroh/numeric-edittext) by [@hidroh](https://github.com/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](https://github.com/akamud/NumericEditText-Xamarin.Android/blob/master/LICENSE)