Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate button when write text in editText android

I have a layout where I have some edittexts and one button. This button is setEnabled(false). How I can change his status when user write some text in edittext? I want to when user put some text to edit text button setEnabled on true?

like image 651
user1302569 Avatar asked Jan 29 '26 18:01

user1302569


2 Answers

You need to use TextWatcher:

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

                // TODO Auto-generated method stub
            }

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

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        });
like image 101
Shrikant Ballal Avatar answered Feb 01 '26 09:02

Shrikant Ballal


Its a good idea to also notify user why the Button is disabled.

Use a TextWatcher to disable/enable button and also set a error hint for EditText:

mEditText.addTextChangedListener(new EmptyValidator());

private class EmptyValidator implements TextWatcher {

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

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

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.length() == 0) {
                mEditText.setError("Required");
                mButton.setEnabled(false);
            } else {
                mEditText.setError(null);
                mButton.setEnabled(true);
            }
        }

    }
like image 38
S.D. Avatar answered Feb 01 '26 11:02

S.D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!