Enable and disable Button according to the text in EditText in Android

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?

  • TTS-UtteranceProgressListener not being called
  • SHA1 certificate fingerprint
  • Testing Accessibility on Emulated Device (Marshmallow)
  • Button style in AlertDialogs
  • Getting orientation of Android device
  • Creating unbound service in Kotlin
  • Related posts:

    Animation.setFillAfter/Before - Do they work/What are they for?
    How do I write an android JUnit test when my activity relies on extras passed through an Intent?
    EditText: Avoid getting focus on setText()
    android.database.sqlite.SQLiteDatabase.rawQuery() is not updating a DATETIME column with a SQLite da...
    Retrieve distance from visible part of Google map
    Is it possible to compile LLVM libraries to android/ARM
  • Security of Android assets folder
  • Realm, RxJava, asObservable() and doOnUnsubscribe()
  • Android Activity Transition in a ListView
  • Setting header colour in the new Material DatePicker
  • Find SD card volume label on Android
  • Android LVL Signature Verification Failed
  • 3 Solutions collect form web for “Enable and disable Button according to the text in EditText in Android”

    You have to addTextChangedListener to your EdiText

    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.

    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
    }  
    

    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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.