3

I'm using an Alert Dialog on my application, but it keeps hiding when the users touches outside it. Here is my code:

public class DialogMessageEnd extends DialogFragment
{
    String winner;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        Snooker_Scoreboard ss = new Snooker_Scoreboard();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(false);
        builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
                .setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent i = new Intent(getContext(),PlayerSelection.class);
                        startActivity(i);
                    }
                });



        // Create the AlertDialog object and return it
        return builder.create();
    }

}

As you can see, I used

builder.setCancelable(false);

but it still doesn't solve the probem. Can you help me? thanks

Pino
  • 619
  • 1
  • 8
  • 25
  • 2
    Did you take a look at `Dialog.setCanceledOnTouchOutside` method? – Selvin Feb 15 '17 at 16:11
  • Selvin if you at least would read and understand, you would nothice that I am using Alert Dialog which does not have the setCanceledOnTouchOutside method. – Pino Feb 15 '17 at 16:26
  • @Pino AlertDialog does indeed have setCanceledOnTOuchOutside – tyczj Feb 15 '17 at 16:35

3 Answers3

36

Use setCanceledOnTouchOutside(false) for preventing the dismiss on touching outside of alert dialog.

setCancelable(false) is used for preventing the dismiss on pressing the back button.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    Snooker_Scoreboard ss = new Snooker_Scoreboard();
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(false);
    builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
            .setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent i = new Intent(getContext(),PlayerSelection.class);
                    startActivity(i);
                }
            });



    // Create the AlertDialog object and return it
    Dialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);         
    return dialog;
}
Sanjeet
  • 2,385
  • 1
  • 13
  • 22
6

Add

AlertDialog alertDialog = builder.show();

alertDialog.setCanceledOnTouchOutside(false);

in your code

Najeeb Idrees
  • 453
  • 6
  • 15
2

You can override the default function for the Dialogue and assure nothing happens. Should work fine.

@Override
public boolean onTouchEvent(MotionEvent event) {
    // If we've received a touch notification that the user has touched
    // outside the app, finish the activity.
    if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
    // Do Something or not...
        return true;
    }
    return false;
}

Or for better practice:

builder.setCanceledOnTouchOutside(false)
Shahzad Afridi
  • 2,058
  • 26
  • 24
Luís Gonçalves
  • 338
  • 5
  • 17