views:

217

answers:

1

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:

  • Disable the submit button until the text box is not empty (in some onChange() type handler for the text box)
    • How do you know when the content of the text box changes?
    • How do you get a reference to the AlertDialog's button object?
  • Check in the submit button's onClick() and cancel the dismissing of the dialog if it's empty.
    • Is it possible to do this with an AlertDialog's button? The dialog dismisses without manually calling dismiss() or cancel(), so I'm not sure...

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.

+1  A: 

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
   }

});
Roflcoptr