0

I'm making a validation form with EditText. I have three EditText boxes and I want to make sure the user types something on ed1 before going to ed2. So, I want to block the user from going to ed2 unless they put something in ed1. If ed1 is null, the cursor won't go to ed2. Can someone help me?

final EditText ed1 = (EditText) findViewById(R.id.one);
final EditText ed2 = (EditText) findViewById(R.id.two);

TextWatcher watcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
        if (ed1.getText().toString().equals("")) {
            ed2.requestFocus();
        }
    }
};
Floern
  • 33,559
  • 24
  • 104
  • 119
JuJu Juned
  • 134
  • 8
  • you should simply use the `setError` to display message when user press submit otherwise you need to create a chain where every edit text check the non-empty criteria of previous edittext – Pavneet_Singh Jun 30 '17 at 16:56
  • Have u tried my updated answer – Anil Jul 03 '17 at 02:27

2 Answers2

0
  1. Use TextUtil to check for empty inputs.
  2. Use editText.setEnabled() method or editText.setActivated() according to use. Also, use drawable.xml to change the style of the View

        final EditText ed1 = (EditText) findViewById(R.id.one);
        final EditText ed2 = (EditText) findViewById(R.id.two);
    
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(ed1.getText())) {
                    //ed2.requestFocus();
                    ed2.setEnabled(false);
                }
            }
        };
    

Let me know if it is helpful to you.

Krunal Kapadiya
  • 2,853
  • 3
  • 16
  • 37
0

Use like this

Initially make ed2 as disabled

android:enabled="false"

and in class file use

   ed1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

            if (s.length()>0) {
                ed2.setEnabled(true);
            }
            else
            {
                ed2.setEnabled(false);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Anil
  • 1,605
  • 1
  • 14
  • 24
  • that also did not work. it just block the second ed2 if i set it to false. but it does not get activated afterwards. – JuJu Juned Jul 03 '17 at 00:02