Remove zeros from first of numbers in Android EditText

1.2k views Asked by At

In Android EditText, inputType is number. I don't want to have zero(s) before other numbers. I mean when user enters 0 and after that enters 3, it will show 03 but I want to have 3. I know I can use textwatcher. But is there better solution?!

1

There are 1 answers

3
Francesco Bocci On BEST ANSWER

There is an alternative way in which you can use textWatcher .Check length of editable text inside afterTextChange method. If length is 1 and string is 0 than remove 0 .

EDIT:

editTextPack.addTextChangedListener(new TextWatcher() {

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

                        if (s.toString().trim().length() == 1 && s.toString().trim().equals("0")) {

                        editTextPack.setText("");                             
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                                                  int after) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub
                    }
                });

Something like that.