Praveen
Praveen

Reputation: 91175

Entering Decimal values in Edittext?

I have a edit text. i want to enter the decimal values in it.

that is when i have enter a first number. it should be like this: .01

then i have enter a second number. it should be like this: .12

then for third one. it should be like this: 1.23

it will go like this.... how to implement this scenario. Any Idea?

Upvotes: 3

Views: 841

Answers (2)

runor49
runor49

Reputation: 3890

You should add a TextWatcher to the EditText. This will notify you when the text changes.

When you know the text has changed, you can run it through a DecimalFormat:

DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMaximumFractionDigits(2);

Upvotes: 5

Yoni Samlan
Yoni Samlan

Reputation: 38065

You can use a TextWatcher to look for modifications to the value in the field and insert a decimal point in the appropriate position as they type. See this SO question for an example of using a TextWatcher.

Upvotes: 1

Related Questions