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 ?
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
}
});
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
}
});
}
}
Simple just check the condition in onCreate
if (et.getText().toString().trim().equals("")){
button.setEnabled(false);
}
else{
button.setEnabled(true);
}
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) {
}
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With