0

I want that my EditText will accept float value from a given range and accepts only two digit after decimal point, say from 0 to 99.99

In my xml, I have included

android:inputType="numberDecimal"

kindly help.

AndyFaizan
  • 1,833
  • 21
  • 30
Sunil
  • 13
  • 1
  • 5

1 Answers1

0

You should add addTextChangedListener to your editText something like this:

editText.addTextChangedListener(new TextWatcher() {


            @Override
            public void afterTextChanged(Editable arg0) {
            }

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

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {

            }
        });

According to your logic you can implement the input check. for example:

public void afterTextChanged(Editable s) {

    String number = editText.getText().toString();
    if (number .length != 0) {
        int number  = Integer.parseInt(added_number);

        if (number > 99)
            Toast.makeText(getApplicationContext, "Not more than 99", Toast.LENGTH_SHORT).show();
    }

}

hope it helps. If you are looking to test the input on the fly, you should override onTextChanged. i have some example code if you like

alexk
  • 213
  • 1
  • 10