Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent ProgressDialog from getting dismissed by onClick

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();
like image 765
user2224350 Avatar asked Sep 02 '13 18:09

user2224350


People also ask

How to set Message in progress dialog?

ProgressDialog progressDialog = new ProgressDialog(this); 1. setTitle (CharSequence title) – This component is used to set the title of the progress dialog. 2. setMessage (CharSequence message) – This component displays the required message in the progress dialog. // Setting Message progressDialog.setMessage("Loading...");

How to prevent dialog from closing when Button is clicked using Kotlin?

This example demonstrates how to prevent a dialog from closing when a button is clicked using Kotlin. Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.kt

How to create progressdialog example in Android Studio?

Below you can download code, see final output and step by step explanation of ProgressDialog example in Android Studio. Step 1: Create a new project and name it ProgressDialogExample. Step 2: Open res -> layout -> activity_main. xml (or) main. xml and add following code:

How to stop the event from propagating with inline onclick attribute?

Use HTML DOM stopPropagation () method to stop the event from propagating with inline onclick attribute which is described below:


2 Answers

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".

    }

});
like image 187
Joshua Pinter Avatar answered Nov 15 '22 08:11

Joshua Pinter


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();
like image 22
Karl Avatar answered Nov 15 '22 07:11

Karl