3

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?

Praveen
  • 90,477
  • 74
  • 177
  • 219

2 Answers2

5

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);
runor49
  • 3,870
  • 1
  • 21
  • 18
  • Thanks for your efforts. But i could notfind a better logic to implement this scneario. Please Help on this. – Praveen Feb 08 '11 at 08:56
1

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.

Community
  • 1
  • 1
Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
  • I believe there's sample code in the SO question I linked to. There are a ton of examples of usage of TextWatcher on Google Code also: http://code.google.com/query/#q=textwatcher – Yoni Samlan Feb 01 '11 at 16:55
  • Thanks for your efforts. But i could notfind a better logic to implement this scneario. Please Help on this. – Praveen Feb 08 '11 at 08:57