Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Dialog Input

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.

like image 993
stormin986 Avatar asked May 09 '10 21:05

stormin986


People also ask

What does validating user input mean?

Input Validation, also known as data validation, is the testing of any input (or data) provided by a user or application against expected criteria. Input validation prevents malicious or poorly qualified data from entering an information system.

How is input validated?

Input validation is the process of testing input received by the application for compliance against a standard defined within the application. It can be as simple as strictly typing a parameter and as complex as using regular expressions or business logic to validate input.

How do you inflate dialog?

To inflate the layout in your DialogFragment , get a LayoutInflater with getLayoutInflater() and call inflate() , where the first parameter is the layout resource ID and the second parameter is a parent view for the layout. You can then call setView() to place the layout in the dialog.


2 Answers

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) {

    }
});
like image 183
ebernie Avatar answered Nov 15 '22 17:11

ebernie


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
   }

});
like image 22
RoflcoptrException Avatar answered Nov 15 '22 19:11

RoflcoptrException