Giter VIP home page Giter VIP logo

numericedittext-xamarin.android's Introduction

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

numericedittext-xamarin.android's People

Contributors

akamud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

numericedittext-xamarin.android's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.