Dinesh Neupane
Dinesh Neupane

Reputation: 402

EditText setEnabled() not working

I've two EditText fields and I want to enable the second EditText only after user inputs some value in first EditText. I tried the following but it isn't giving expected results. The second editText remains disabled even when the user inputs the value in the first.

String et1Value = et1.getText().toString();
if(!et1Value.equals("")){
et2.setEnabled(false);
}

Upvotes: 1

Views: 684

Answers (1)

Tomin B Azhakathu
Tomin B Azhakathu

Reputation: 2686

Define this under your edittext in onCreate

try the following code with text change listener

  et1.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int aft )                                                                          
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
         String et1Value = et1.getText().toString();
         if(!et1Value.equals("")){
         et2.setEnabled(true);
        }
        }
    });

Upvotes: 4

Related Questions