Reputation: 47
I have two EditTexts and a Button in my layout
an EditText called UserName and another called Password and the Button is called Login
the Button is disabled at the start
what I want is that when ever anything is inputted in EditText UserName and Password the Button should be Enabled
here is what i tried
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(!TextUtils.isEmpty(UserName.getText()) && !TextUtils.isEmpty(PassWord.getText()))
{
Login.setEnabled(true);
return true;
}
return false;
when I run this code the Login Button start as disabled and it stays disabled when I input something in both the EditTexts without clicking the Enter Button
it Only works when I click the Enter Button
is there a way to make it work when any button in the keypad is clicked and both of the EditTexts not empty
Thanks for help
Upvotes: 0
Views: 958
Reputation: 546
You can use TextChangedListener
class for EditTexts
to listen for events, when the text changes in them. I have a code for this in one of my projects:
private boolean heightFilled;
private boolean weightFilled;
private void setInputListeners() {
etHeight.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
heightFilled = s.length() > 0;
calculateButton.setEnabled(heightFilled && weightFilled);
}
@Override
public void afterTextChanged(Editable s) {
}
});
etWeight.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
weightFilled = s.length() > 0;
calculateButton.setEnabled(heightFilled && weightFilled);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
This should work, if you change the names of the Button
and the EditText
s.
Upvotes: 3
Reputation: 131
You need to use a textchanged listener. From in there you can check as typing if both have an input and call code for activating your button there after checks.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//enter code here to check if your edittexts have content
//then do stuff with your button
}
}
});
Upvotes: 1