Rohit Sharma Rohit Sharma - 26 days ago 12
Android Question

Disable 0 key on softkeyboard for first time

I want to disable the number

0
key on soft keyboard for first time when an user clicks on
EditText
.

The
EditText
has an
input type
number
and i have to input a ten digit phone number where the first digit should not be zero.

Please suggest me how to achieve this?

Answer Source
    boolean changing = false;

    EditText edit = (EditText)findViewById(R.id.medittext);
    edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
             if (!changing && edit.getText().toString().startWith("0")){
                changing = true;
                edit.setText(edit.getText().toString().replace("0", ""));
             }
             changing = false; 
        }

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

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

    });