2

I have 6 edittexts (will be using for OTP)
Each edittext will only allow 1 character and then transfer to another edittext

Here is the sample code:

et1.requestFocus()

et1.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start,int before, int count {
    if(et1.getText().toString().length()==size){
        et2.requestFocus();
    }
}
public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {}

public void afterTextChanged(Editable s) {
     et1.setTransformationMethod(new PasswordTransformationMethod());
}

});

What happens is, the et1, doesn't mask itself as dot/asterisk but the focus is transferred to et2.
But whenever I tap it again, it masks itself.

Edit: Already added inputType="numberPassword" in my XML but still not working

MaChee Neraid
  • 603
  • 1
  • 7
  • 17

1 Answers1

1

try this way

xml

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/otp_first_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_second_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_third_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />

                <EditText
                    android:id="@+id/otp_fourth_et"
                    android:layout_width="45sp"
                    android:layout_height="45sp"
                    android:layout_marginBottom="10dp"
                    android:digits="0123456789"
                    android:drawablePadding="7dp"
                    android:gravity="center"
                    android:imeOptions="actionDone"
                    android:inputType="number"
                    android:maxLength="1"
                    android:paddingLeft="10dp"
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:textCursorDrawable="@drawable/bg_edit_text_cursor" />
            </LinearLayout>

java

EditText otpEditTextFirst, otpEditTextSecond, otpEditTextThird, otpEditTextFourth;

otpEditTextFirst = (EditText) findViewById(R.id.otp_first_et);
        otpEditTextSecond = (EditText) findViewById(R.id.otp_second_et);
        otpEditTextThird = (EditText) findViewById(R.id.otp_third_et);
        otpEditTextFourth = (EditText) findViewById(R.id.otp_fourth_et);


otpEditTextFirst.addTextChangedListener(new GenericTextWatcher(otpEditTextFirst));
        otpEditTextSecond.addTextChangedListener(new GenericTextWatcher(otpEditTextSecond));
        otpEditTextThird.addTextChangedListener(new GenericTextWatcher(otpEditTextThird));
        otpEditTextFourth.addTextChangedListener(new GenericTextWatcher(otpEditTextFourth));


class GenericTextWatcher implements TextWatcher {
        private View view;

        private GenericTextWatcher(View view) {
            this.view = view;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub
            String text = editable.toString();
            switch (view.getId()) {

                case R.id.otp_first_et:

                    if (text.length() == 1)
                        otpEditTextSecond.requestFocus();


                    break;
                case R.id.otp_second_et:

                    if (text.length() == 1)
                        otpEditTextThird.requestFocus();

                    else
                        otpEditTextFirst.requestFocus();
                    break;
                case R.id.otp_third_et:

                    if (text.length() == 1)
                        otpEditTextFourth.requestFocus();

                    else
                        otpEditTextSecond.requestFocus();
                    break;
                case R.id.otp_fourth_et:
                    if (text.length() == 0)
                        otpEditTextThird.requestFocus();


                    break;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    }
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39