I am implementing an OTP screen with six EditText.I want to make single text watcher for all fields.I searched for answers but they are not using data binding.I want to implement it using data binding in my code.how can I do it please help?
bottomSheetLoginBinding.otpOne.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpOne));
bottomSheetLoginBinding.otpTwo.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpTwo));
bottomSheetLoginBinding.otpThree.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpThree));
bottomSheetLoginBinding.otpFour.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpFour));
bottomSheetLoginBinding.otpFive.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpFive));
bottomSheetLoginBinding.otpSix.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpSix));
This is my TextWatcher Class public 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();
boolean allOtherFilled = false;
switch (view.getId()) {
case R.id.otpOne:
if (text.length() == 1)
bottomSheetLoginBinding.otpTwo.requestFocus();
break;
case R.id.otpTwo:
if (text.length() == 1)
bottomSheetLoginBinding.otpThree.requestFocus();
else if (text.length() == 0)
bottomSheetLoginBinding.otpOne.requestFocus();
break;
case R.id.otpThree:
if (text.length() == 1)
bottomSheetLoginBinding.otpFour.requestFocus();
else if (text.length() == 0)
bottomSheetLoginBinding.otpTwo.requestFocus();
break;
case R.id.otpFour:
if (text.length() == 1)
bottomSheetLoginBinding.otpFive.requestFocus();
else if (text.length() == 0)
bottomSheetLoginBinding.otpThree.requestFocus();
break;
case R.id.otpFive:
if (text.length() == 1)
bottomSheetLoginBinding.otpSix.requestFocus();
else if (text.length() == 0)
bottomSheetLoginBinding.otpFour.requestFocus();
break;
case R.id.otpSix:
if (text.length() == 0)
bottomSheetLoginBinding.otpFive.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
}
}
I have done this code in my activity class.But I think it does not achieve MVVM completely.How can I do this in ViewModel class.