OTP Next/Back Pressed Change Its Focus

Description: When User enter one character in Edit text of otp, the cursor position has change to next field and when user press back the focus will change to previous field, To Achieve this Add the Following code(here i am using 6 digit otp with ed1,ed2,ed3,ed4,ed5,ed6 as Edit Text Fields)

Code:

;ed1.setOnKeyListener((v, keyCode, event) -> {
        if(ed1.getText().toString().length()>0){
            ed2.requestFocus();
        }
    return false;
});
ed2.setOnKeyListener((v, keyCode, event) -> {
    if(event.getAction() == KeyEvent.ACTION_DOWN  && event.getKeyCode() == KeyEvent.KEYCODE_DEL  && ed2.getText().toString().length() ==0) {
        ed1.setText("");
        ed1.requestFocus();
    }else{
        if(ed2.getText().toString().length()>0){
            ed3.requestFocus();
        }
    }
    return false;
});
ed3.setOnKeyListener((v, keyCode, event) -> {
    Log.d("sdfs","key3 "+keyCode);
    if(event.getAction() == KeyEvent.ACTION_DOWN  && event.getKeyCode() == KeyEvent.KEYCODE_DEL  && ed3.getText().toString().length() ==0) {
        Log.d("sdfs","key3 back "+keyCode);
        ed2.setText("");
        ed2.requestFocus();

    }else{
        if(ed3.getText().toString().length()>0){
            ed4.requestFocus();
        }
    }
    return false;
});
ed4.setOnKeyListener((v, keyCode, event) -> {
    Log.d("sdfs","key4  "+keyCode);
    if(event.getAction() == KeyEvent.ACTION_DOWN  && event.getKeyCode() == KeyEvent.KEYCODE_DEL  && ed4.getText().toString().length() ==0 && ed4.hasFocus()) {
        Log.d("sdfs","key4  "+keyCode);
        ed3.setText("");
        ed3.requestFocus();

    }else{
        if(ed4.getText().toString().length()>0){
            ed5.requestFocus();
        }
    }
    return false;
});
ed5.setOnKeyListener((v, keyCode, event) -> {
    Log.d("sdfs","key5 "+keyCode);
    if(event.getAction() == KeyEvent.ACTION_DOWN  && event.getKeyCode() == KeyEvent.KEYCODE_DEL && ed5.getText().toString().length() ==0) {
        ed4.setText("");
        ed4.requestFocus()

    }else{
        if(ed5.getText().toString().length()>0){
            ed6.requestFocus();
        }
    }
    return false;
});
ed6.setOnKeyListener((v, keyCode, event) -> {
    Log.d("sdfs","key6  "+keyCode);
    if(event.getAction() == KeyEvent.ACTION_DOWN  && event.getKeyCode() == KeyEvent.KEYCODE_DEL  && ed6.getText().toString().length() ==0) {
        ed5.setText("");
        ed5.requestFocus();

    }else{
        if(ed6.getText().toString().length()>0){
            
        }
    }
    return false;
});
ed1.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed1.setText("");
    }
});
ed2.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed2.setText("");
    }
});
ed3.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed3.setText("");
    }
});
ed4.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed4.setText("");
    }
});
ed5.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed5.setText("");
    }
});
ed6.setOnFocusChangeListener((view, hasFocus) -> {
    if (hasFocus) {
        ed6.setText("");
    }
});


Sign In or Register to comment.