0

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.

  • Your question is not clear to me. You want a single textwatcher and bind it in XML with databinding? (because this is not possible), while using a single `TextWatcher` is possible just by using the same textwatcher for all the `EditTexts` Could you add an example of what you want to achieve? – MatPag Oct 28 '19 at 09:57
  • [https://stackoverflow.com/questions/38872546/edit-text-for-otp-with-each-letter-in-separate-positions] Please check the link I want the same functionality but I want to use data binding instead of this.@MatPag – Lalit Sharma Oct 28 '19 at 10:03
  • Could you describe what is the use case for using single textwatcher? – Sebastian Pakieła Oct 28 '19 at 10:07
  • But you need the requestFocus functionality only? If so you don't have any silver bullets using DataBinding. You just need to code it manually because there are no XML properties to bind it – MatPag Oct 28 '19 at 10:09
  • bottomSheetLoginBinding.otpOne.addTextChangedListener(new GenericTextWatcher(bottomSheetLoginBinding.otpOne)) public class GenericTextWatcher implements TextWatcher { private View view; private GenericTextWatcher(View view) { this.view = view; }@Override public void afterTextChanged(Editable editable) { String text = editable.toString(); switch (view.getId()) case R.id.otpOne: break; I did like this in my activity class Is it the right way.Sharing some code.I wanted to do it in ViewModel class. – Lalit Sharma Oct 28 '19 at 10:30

0 Answers0