Prevent ProgressDialog from getting dismissed by onClick
Asked Answered
B

3

5

I' using ProgressDialog to show the user that he has to wait and to make the surface of my app "untouchable" while the user has to wait. I added a Button to the progressDialog which should start some actions if certain conditions are true. The problem is that everytime the user press the button the progressDialog gets automatically dismissed. Even if no action is triggered. How can I prevent the progressDialog from getting dismissed when onClick is called?

thx & regards

EDIT:

    connectingDialog = new ProgressDialog(this);
    connectingDialog.setCancelable(false);
    connectingDialog.setCanceledOnTouchOutside(false);
    connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            /*if(isPrepared)
                startGame();*/
            connectingDialog.show(); // does not work, too
        }

    });
    connectingDialog.show();
Barkeeper answered 2/9, 2013 at 18:31 Comment(0)
X
9

Set the OnClickListener After the ProgressDialog is Shown.

Unlike some of the other Dialogs available in Android, ProgressDialog does not have a setNeutralButton() method so you need to set the onClickListener after the ProgressDialog has been shown, like so:

connectingDialog = new ProgressDialog(this);

connectingDialog.setCancelable(false);
connectingDialog.setCanceledOnTouchOutside(false);

// Create the button but set the listener to a null object.
connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start", 
        (DialogInterface.OnClickListener) null )

// Show the dialog so we can then get the button from the view.
connectingDialog.show();

// Get the button from the view.
Button dialogButton = connectingDialog.getButton( DialogInterface.BUTTON_NEUTRAL );

// Set the onClickListener here, in the view.
dialogButton.setOnClickListener( new View.OnClickListener() {

    @Override
    public void onClick ( View view ) {

        if (isPrepared) {
             connectingDialog.dismiss();
             startGame();
        }

        // Dialog will not get dismissed unless "isPrepared".

    }

});
Xebec answered 19/7, 2014 at 18:2 Comment(0)
S
1

The problem you are having is that ProgressDialog inherits from Dialog/AlertDialog and by default they get dimissed when you press a button on the dialog, regardless of which button it is (positive/negative etc)

The way around this is to intercept the button and override it's listener, but you can only do this after the dialog has been created.

There may be other ways to do this, but I've used this method successfully. Here's an example in code:

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String username = user.getText().toString();
                    String password = pass.getText().toString();
                    if (username != null && username.length() > 0 && password != null && password.length() > 0) {
                        // store credentials in preferences here
                        dialog.dismiss()
                    } else {
                        Toast.makeText(getActivity(), "Username and/or password cannot be blank.", Toast.LENGTH_SHORT).show();
                        user.requestFocus();
                    }
                }
            });
        }
    });

As you can see, I use this so that I can validate that my user has actually entered something in some EditText's before dismissing the dialog, otherwise, prompt them for input.

EDIT:

Worth noting, you need to pass the button a null OnClickListener initially:

    final AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle("Enter Credentials")
            .setView(v)
            .setPositiveButton("OK", null)
            .create();
Scrod answered 2/9, 2013 at 23:15 Comment(4)
+1. This is the way I did too. Just one thing: use android.R.string.ok instead of "OK"Pelops
Noted. I know hardcoded strings are a bad practice and I'll get round to replacing them all eventually...Scrod
But I cannot call progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", null), because its marked as an error..?Barkeeper
@Barkeeper Set (DialogInterface.OnClickListener) null instead of null.Tennant
G
0

Add connectingDialog.setCancelable(false); was enough for me on Android 4.2.2

Gav answered 26/3, 2015 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.