0

I am using the following code to show an AlertDialog and prompt the user to press "Retry". The dialog should remain on-screen until connection is available. The app works correctly in that when network is unavailable the dialog appears.

mProgressBar = new ProgressBar(MainGroupActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
        builder.setTitle("Not Connected")
                .setIcon(R.drawable.disconnect)
                .setView(mProgressBar)
                .setMessage("You are not connected to the Internet")
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Retry connection
                        if(isNetworkAvailable ())
                            mDialog.dismiss();
                    }
                });
        mDialog = builder.create();
        if( !isNetworkAvailable() )
            mDialog.show();

The problem is that the dialog is dismissed as soon as I touch somewhere on the screen or press Retry! How can I prevent that?

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

4 Answers4

2

You can use setCanceledOnTouchOutside(false) .

 AlertDialog mDialog = builder.create();
    mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();

But that will prevent only outside touch not Positive or negative button click . Its AlertDialog's default behavior to dismiss the dialog on any button click whether you call dismiss() or not . So if you want to over ride this behavior you have do something like this.

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Not Connected")
            .setIcon(R.drawable.ic_play_icon)
            .setView(R.layout.item_dialog)
            .setCancelable(false)
            .setMessage("You are not connected to the Internet");
    final AlertDialog mDialog = builder.create();
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialogInterface) {
            Button button =  mDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Do your validations task here

                }
            });
        }
    });
    mDialog.show();
ADM
  • 20,406
  • 11
  • 52
  • 83
1

setCancelable(false) will work for you.

AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
    builder.setTitle("Not Connected")
            .setIcon(R.drawable.disconnect)
            .setView(mProgressBar)
            .setCancelable(false)
            .setMessage("You are not connected to the Internet")
            .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //Retry connection
                    if(isNetworkAvailable ())
                        mDialog.dismiss();
                }
            });
Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
0

Add setCancelable(false) on your construction of alertDialog.

mProgressBar = new ProgressBar(MainGroupActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainGroupActivity.this);
        builder.setTitle("Not Connected")
                .setIcon(R.drawable.disconnect)
                .setView(mProgressBar)
                .setMessage("You are not connected to the Internet")
                .setCancelable(false)
                .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Retry connection
                        if(isNetworkAvailable ())
                            mDialog.dismiss();
                    }
                });
        mDialog = builder.create();
        if( !isNetworkAvailable() )
            mDialog.show();
Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
0

To prevent dialog box from getting dismissed on back key pressed use this

dialog.setCancelable(false);

And to prevent dialog box from getting dismissed on outside touch use this

 dialog.setCanceledOnTouchOutside(false);

for further help take a look at this question: Prevent Android activity dialog from closing on outside touch

Umair
  • 6,366
  • 15
  • 42
  • 50