Reputation: 7692
I am using an AlertDialog that is very simple, just a custom view with a text box, and the alertdialog positive submit button. I would like to validate that the text box has had text entered before the user dismisses the dialog. I see two possibilities, with questions about each:
Is either of these options possible with an AlertDialog (vs a custom dialog)?
I think the second option would be the simplest, but I'm up for either if it's possible.
Upvotes: 10
Views: 9699
Reputation: 820
I set a TextWatcher to an input field, and then enable the positive button when validation conditions are met. I disable the positive button by default.
AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
final View dialogView = LayoutInflater.from(getSherlockActivity()).inflate(R.layout.create_playlist, null);
final EditText inputField = (EditText) dialogView.findViewById(R.id.edit_newPlaylistName);
... //proceed to setup dialog using builder
final AlertDialog alertDialog = builder.show();
alertDialog.setCanceledOnTouchOutside(true);
final Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
inputField.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) {
// my validation condition
if (inputField.getText().length() > 0) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 8
Reputation: 2715
Here's a way to validate input and keep an Alert dialog on the screen if the input is not valid. Actually it removes the dialog and puts up a new copy of it.
In your setPostiveButton's onClick function, do your validation. If things are not as they should be, show the user a Toast. Then call removeDialog on your dialog. Then - and this is the tricky part, asynchronously call showDialog on your dialog (with args if applicable). Also: In order not to lose the user's input, you should put the values that they entered into the bundle which you invoke the dialog with. And of course your setup code for the dialog needs to look for those values in the bundle and prepopulate the dialog fields appropriately.
So your code would look something like this:
alert.setPositiveButton(id,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
if ((your validation conditions)) {
// validation conditions not satisfied
removeDialog(myDialogId);
Toast.makeText(blah blah).show();
// now create a new args bundle for the dialog
Bundle newArgs = new Bundle();
// now copy whatever you need from the args used to invoke to dialog
newArgs.putIntegerArrayList("items", myList);
// now save the user's input in a bundle
newArgs.putString("dialogToFieldContent", toString);
final Bundle finalArgs = newArgs;
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
showDialog(myDialogId, finalArgs);
}
});
}
else {
// if everything is ok
}
}
});
Upvotes: 1
Reputation: 52229
The easiest way i think is to defined your own dialog in a xml file. Then you can display it very simple (in this example somewhere in the activity class):
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.your_dialog_file);
Button yourButton = dialog.findViewById(R.id.yourButton);
final EditText text = dialog.findViewById(R.id.yourTextEdit);
yourButton.setOnClickListener( {
public void onClick(View view) {
if ( ! (text.getText().toString.equals(""))) {
//go on here and dismiss dialog
}
});
Upvotes: 4