Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog with DialogFragment: don't close the dialog even if OK is clicked

I have an AlertDialog with a custom layout (just an EditText) and I want to validate the data when the OK button is clicked. If the validation fails I don't want to close the dialog.

I'm using dialog's default buttons (positive and negative). If I use "setPositiveButton("", new DialogInterface.OnClickListener() ..." the dialog is always closed. I've seen several posts and they said that the onClick Listener should be override, but I can't get it working. This is the code I found:

Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

Since it says it should be done AFTER showing the dialog I placed this code inside my activity, not inside my DialogFragment, but if I use mDialogFragment.getDialog() it always returns null.

This is a part of my Dialog Fragment:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.new);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    dialogView = inflater.inflate(R.layout.edit_license, null);

    builder.setView(dialogView)

    // Add action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MyDialogFragment.this.getDialog().cancel();
        }
    }); 

    return builder.create();

}

And in my Activity I do the following:

DialogFragment dialog = new MyDialogFragment(true, null);
dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");

AlertDialog alertDialog = (AlertDialog)dialog.getDialog();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setOnClickListener(new CustomListener(alertDialog));

... 

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {
        ...
    }
}

Like I said, (AlertDialog)dialog.getDialog(); always returns null. Why is that? How can I avoid closing the dialog if the validation is not ok?

Thanks!

like image 280
PX Developer Avatar asked Feb 12 '13 11:02

PX Developer


People also ask

How do you dismiss dialog with click on outside of dialog?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the CloseOnEscape property value to false to prevent closing of the dialog when pressing Esc key.

Can you have an alert dialog without any buttons if not why?

You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.

How do I turn off alerts in dialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


2 Answers

This solution works for me. Dialog is already visible in onResume so this is a good place to get access to buttons and replace listeners.

public class XYZFragment extends DialogFragment {
   ...

    @Override
    public void onResume() {
        super.onResume();

        AlertDialog dialog = (AlertDialog)getDialog();

        Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {                  
            @Override
            public void onClick(View onClick) {
            ...
            }
        });
    }
}
like image 149
Michal Faber Avatar answered Oct 17 '22 23:10

Michal Faber


here is some text from reference of DialogFragment:

A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

Instead of calling getDialog().dismiss() you should do dismiss()

like image 31
Tobrun Avatar answered Oct 17 '22 23:10

Tobrun