Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Button if EditText is empty ?

Tags:

android

button

I have an EditText and a Button in my application.

When the button is clicked,the text entered in the EditText is added to a ListView.

I want to disable the Button if the EditText is empty.How to do this ?

This is my code for button click

ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
            imb.setOnClickListener(new OnClickListener()
            {
             @Override
             public void onClick(View arg0) 
             { 
                 EditText et = (EditText)findViewById(R.id.EditText1);

                  String str = et.getText().toString();
                  web1.add(str);
                  Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
                  adapter1.notifyDataSetChanged();
                  et.setText("");

                    }
            });
            }

How can i do this ?

like image 982
Nevaeh Avatar asked Mar 27 '14 06:03

Nevaeh


4 Answers

    editText1.addTextChangedListener(new TextWatcher() {

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

           if(s.toString().trim().length()==0){
                button.setEnabled(false);
              } else {
                button.setEnabled(true);
              }
        }

        @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 171
drulabs Avatar answered Nov 14 '22 08:11

drulabs


Use TextChangedListener and initially disable ImageButton in onCreate().

Try this

public class MyActivity extends Activity {

ImageButton imb;
EditText et;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imb = (ImageButton) findViewById(R.id.btn_send);
    et = (EditText) findViewById(R.id.EditText1);

    imb.setEnabled(false); // set button disable initially

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

            if (s.toString().equals("")) {
                imb.setEnabled(false);
            } else {
                imb.setEnabled(true);
            }
        }

        @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 31
Chirag Ghori Avatar answered Nov 14 '22 07:11

Chirag Ghori


Simple just check the condition in onCreate

if (et.getText().toString().trim().equals("")){
  button.setEnabled(false);
}
else{
button.setEnabled(true);
}
like image 24
InnocentKiller Avatar answered Nov 14 '22 07:11

InnocentKiller


Here's a super simple answer in Kotlin.

Just replace 'EditText' and 'Button' with your own.

Button.isEnabled = false
        EditText.addTextChangedListener(object: TextWatcher {
            override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
                Button.isEnabled = s.toString().trim{ it <= ' ' }.isNotEmpty()
            }
            override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
                                           after:Int) {
            }
            override fun afterTextChanged(s: Editable) {
            }
        })

For Multiple EditTexts go

Button.isEnabled = false
            val editTexts = listOf(editText1, editText2, editText3, editText4, editText5, editText6)
for (editText in editTexts) {
            editText.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                var et1 = editText1.text.toString().trim()
                var et2 = editText2.text.toString().trim()
                var et3 = editText3.text.toString().trim()
                var et4 = editText4.text.toString().trim()
                var et5 = editText5.text.toString().trim()
                var et6 = editText6.text.toString().trim()

                computeBtn.isEnabled = et1.isNotEmpty()
                        && et2.isNotEmpty()
                        && et3.isNotEmpty()
                        && et4.isNotEmpty()
                        && et5.isNotEmpty()
                        && et6.isNotEmpty()
            }

            override fun beforeTextChanged(
                s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun afterTextChanged(
                s: Editable) {
            }
        })
        }
like image 41
Stevo Avatar answered Nov 14 '22 07:11

Stevo