MMG
MMG

Reputation: 3268

Remove zeros from first of numbers in Android EditText

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?!

Upvotes: 1

Views: 1478

Answers (1)

Francesco Bocci
Francesco Bocci

Reputation: 867

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.

Upvotes: 1

Related Questions