-1

I am working in an Android application.In my application there is a edit text. in that edit text if user enters 12, it has to change dynamically as 12.00. which means it only accept decimal values. if user enters as 12.3 then it should become 12.30.and if 12.35 then it should be 12.35 only. it will not allow user to enter more than two after dot. Please help in this scenario?

JK.C
  • 97
  • 2
  • 5

1 Answers1

1

Use a TextWatcher to handle dinamically the input, then inside the TextWatcher use something like DecimalFormat to change the text.

yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {
        // do something here
        DecimalFormat form = new DecimalFormat("0.00");
        String FormattedText=form.format(s.toString());
    }
});
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • Thank you for your response. i can restrict him by entering only two number after dot. but if user enter 12 it should display as 12.00? – JK.C Jun 13 '14 at 10:39
  • Do something always in the same method. For example check for the dot, if not exists, add it with the zeros (or something like that). – Enrichman Jun 13 '14 at 10:44