Reputation: 172
I have an alert dialog
and I have an input from the alert dialog
. The current issue is I cant handle the input from the alert dialog
if the user didnt input any text and click ok button. How to set the textfield
showing error if user didnt input any text and click OK button and without closing the alert dialog
. Below is my code.
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Save QR Code");
builder.setMessage("Please enter the name for the QR Code.");
builder.setCancelable(true);
// Set up the input
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT );
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Here is the part
m_Text = input.getText().toString().trim();
if(TextUtils.isEmpty(m_Text)){
input.setError("Please Enter Name of the Image QR Code");
}else {
//do smtg
}
}
});
builder.show();
Upvotes: 1
Views: 342
Reputation: 11419
You should set the listener later:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Save QR Code");
builder.setMessage("Please enter the name for the QR Code.");
builder.setCancelable(true);
// Set up the input
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT );
builder.setView(input);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view1 -> {
m_Text = input.getText().toString().trim();
if(TextUtils.isEmpty(m_Text)){
input.setError("Please Enter Name of the Image QR Code");
} else {
dialog.dismiss();
}
});
The problem with your approach is that AlertDialog will still use its listener as a kind of decorator of yours. So it will call your listener but then it will still close the dialog. If you want to prevent it, you need to overwrite the internal listener with yours after the dialog is shown.
Upvotes: 1
Reputation: 3212
Add a TextWatcher
to the EditText
. When the text is empty, show the error message. You can even hide the PositiveButton
from within the TextWatcher
to prevent the user from hitting it when the input is invalid.
myEditText.addTextChangedListener(new TextWatcher() {
boolean bIgnore = false; // indicates if the change was made by the TextWatcher itself.
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (bIgnore)
return;
bIgnore = true; // prevent infinite loop
if (inputValidated(myEditText.getText().toString()) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
errorTextView.setVisibility(View.INVISIBLE);
} else {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
errorTextView.setVisibility(View.VISIBLE);
}
bIgnore = false; // release, so the TextWatcher start to listen again.
}
});
Upvotes: 1