Search code examples
androidstringbuttonandroid-edittextclickable

Check EditText to enable/disable buttons


I have an EditText that has a stringA stored. When some processes happen, the EditText is updated with a new value (stringB). I have three buttons. I want them disabled while EditText = stringA & updated to enabled when the EditText holds the stringB value.

final EditText latlongText = (EditText) findViewById(R.id.EditText_COord)

This is my EditText. By default, it just holds a string stored in XML as android:hint= "blah blahstringA"

I update it like this in the code:

   latlongText.setText(stringB);

In the onCreate, I have:

 buttonA.setEnabled(false);
 buttonB.setEnabled(false);
 buttonC.setEnabled(false);

I'm not sure where or how to correctly place the code to re-enable these buttons.

Perhaps the "clickable" attribute is also an option? I want them to enable when stringB overwrites the value currently stored as the "android:hint" string.

Thanks


Solution

  • latlongText.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 after) {    
       }
       @Override
       public void afterTextChanged(Editable s) {
            String st=latlongText.getText().toString().trim()
            if(st.equals(stringA)){
               buttonA.setEnabled(false);
               buttonB.setEnabled(false);
               buttonC.setEnabled(false);               
            }
            else if(st.equals(stringB)){
               buttonA.setEnabled(true);
               buttonB.setEnabled(true);
               buttonC.setEnabled(true);
            }
        }
    });