I have an EditText
inside an AlertDialog
. I would like the dialog to be dismissed whenever the word "stop" is detected in the EditText
. I tried calling dismiss()
on the dialog, but it doesn't work :
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog popup = builder.create();
final EditText edit = new EditText(this);
edit.setGravity(Gravity.CENTER);
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence pRequest, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence pRequest, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
String currentText = s.toString().toLowerCase();
if (currentText.contains("stop")) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); // Hide keyboard
popup.dismiss();
}
}
});
builder.setTitle("Value")
.setView(edit)
.show();
Any idea to fix this ?