19

I want to disable by Button if the words in the EditText is less than 3 words, and if the words in the EditText are more than 3 words then I want to enable it so that it can get Clicked.

Can anyone help me here?

THelper
  • 15,333
  • 6
  • 64
  • 104
David Brown
  • 4,783
  • 17
  • 50
  • 75

3 Answers3

38

You have to addTextChangedListener to your EditText

Like this:

yourEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable arg0) {
         enableSubmitIfReady();
      }

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

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

In that method, you should do like this:

 public void enableSubmitIfReady() {

    boolean isReady = yourEditText.getText().toString().length() > 3;    
    yourbutton.setEnabled(isReady);
  }

Hope it helps.

talz
  • 1,004
  • 9
  • 22
Uday
  • 5,933
  • 9
  • 44
  • 76
  • 1
    I think enableSubmitIfReady() should be in onTextChanged – Rohit Nov 22 '11 at 11:55
  • 2
    Good answer except I prefer to see that sort of if-else pattern simplified into: `yourbutton.setEnabled(isReady);` – mharper Sep 01 '13 at 00:00
  • How does this work without a reference to the button? You have the line yourbutton... but yourbutton has no reference anywhere in this code so how can it work? –  Jul 21 '14 at 17:59
  • I think you could simplify this slightly by moving `enableSubmitIfReady()` to `onTextChanged()` and replacing `yourEditText.getText().toString().length()` with `s.length()`. Look [here](http://stackoverflow.com/a/16567135/4682839) – young_souvlaki Jun 06 '16 at 18:57
1

The problem with using afterTextChanged alone is at application start it can't disable the button initially until you start typing to your EditText.

This is how I implemented mine and it works great. Call this method inside your Activity's onCreate method

void watcher(final EditText message_body,final Button Send)
{
    final TextView txt = (TextView) findViewById(R.id.txtCounter);
    message_body.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged(Editable s) 
        { 
            txt.setText(message_body.length() + " / 160"); //This is my textwatcher to update character left in my EditText
            if(message_body.length() == 0)
                Send.setEnabled(false); //disable send button if no text entered 
            else
                Send.setEnabled(true);  //otherwise enable

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){
        }
        public void onTextChanged(CharSequence s, int start, int before, int count){
        }
    }); 
    if(message_body.length() == 0) Send.setEnabled(false);//disable at app start
}  
mboy
  • 744
  • 7
  • 17
1

You can do what @Udaykiran says but use arg0.length() instead.

The Editable also contains the length of the content of the TextEditor that was changed

Mack
  • 2,556
  • 1
  • 26
  • 44
cbw
  • 266
  • 3
  • 6