Mehdi Haghgoo
Mehdi Haghgoo

Reputation: 3484

How to Keep AlertDialog on Screen While Network is Not Available

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?

Upvotes: 0

Views: 380

Answers (4)

SpiritCrusher
SpiritCrusher

Reputation: 21043

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();

Upvotes: 2

Umair
Umair

Reputation: 6426

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

Upvotes: 0

Tenten Ponce
Tenten Ponce

Reputation: 2506

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();

Upvotes: 0

Nouman Ch
Nouman Ch

Reputation: 4121

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();
                }
            });

Upvotes: 1

Related Questions