Android Enable / Disable Button By EditText Text Length

In some android app, button will be enabled only when user input enough characters in EditText, for example user account register app. There are two methods to archive this. Use android.text.TextWatcher or listen android.view.KeyEvent event. This example will tell you how to implement this.

1. Use TextWatcher.

  1. Create an instance of android.text.TextWatcher.
  2. Override it’s afterTextChanged(Editable editable) method.
  3. In that method, get the EditText text length and disable or enable related button accordingly.
  4. Call EditText’s addTextChangedListener(TextWatcher tw) method to add that text watcher to the edit text.

2. Listen KeyEvent For EditText Object.

  1. Create a View.OnKeyListener object.
  2. Override it’s onKey method.
  3. In onKey method check EditText text length and enable or disable button.
  4. Invoke EditText’s setOnKeyListener method to set above OnKeyListener object to it.

Please note KeyEvent has two actions, KeyEvent.ACTION_UP and KeyEvent.ACTION_DOWN. You should distinguish these two actions in your code, otherwise the code will executed twice because each of the two action can trigger the code.

3. Example And Source Code.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/SYu2N2YBzdo

In this example, when EditText text length is bigger than 10, then bottom button will be enabled, otherwise it will be disabled. The button text will also be changed accordingly.

3.1 Activity Java File.

EnableDisableButtonActivity.java

package com.dev2qa.example.widget.edittext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dev2qa.example.R;

public class EnableDisableButtonActivity extends AppCompatActivity {

    private EditText editText;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enable_disable_button);

        setTitle("dev2qa.com - Enable / Disable Button By EditText Text Length.");

        // Get EditText component.
        editText = (EditText)findViewById(R.id.enable_disable_button_edit_text);

        // Get button component.
        button = (Button)findViewById(R.id.enable_disable_button);

        // Use TextWatcher to change button state by EditText text length.
        editText.addTextChangedListener(new TextWatcher() {
            
            // Before EditText text change.
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            // This method is invoked after user input text in EditText.
            @Override
            public void afterTextChanged(Editable editable) {
                processButtonByTextLength();
            }
        });


        // Listen to EditText key event to change button state and text accordingly.
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {

                // Get key action, up or down.
                int action = keyEvent.getAction();

                // Only process key up action, otherwise this listener will be triggered twice because of key down action.
                if(action == KeyEvent.ACTION_UP)
                {
                    processButtonByTextLength();
                }
                return false;
            }
        });

    }

    // Enable or disable and change button text by EditText text length.
    private void processButtonByTextLength()
    {
        String inputText = editText.getText().toString();
        if(inputText.length() > 10)
        {
            button.setText("I Am Enabled.");
            button.setEnabled(true);
        }else
        {
            button.setText("I Am Disabled.");
            button.setEnabled(false);
        }
    }
}

3.2 Layout Xml File.

activity_enable_disable_button.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Below button will be enabled at least you input 10 characters."
        android:textSize="20dp"/>

    <EditText
        android:id="@+id/enable_disable_button_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input text here."
        android:textColorHint="@color/colorGreen"
        android:textColor="@color/colorGreen"
        android:textStyle="bold"
        android:background="@color/colorBlue"
        android:layout_margin="5dp"/>

    <Button
        android:id="@+id/enable_disable_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I Am Disabled"
        android:enabled="false"
        android:textAllCaps="false"/>
</LinearLayout>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.