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();
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...");
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
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:
Use HTML DOM stopPropagation () method to stop the event from propagating with inline onclick attribute which is described below:
OnClickListener
After the ProgressDialog is Shown.Unlike some of the other Dialog
s 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".
}
});
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With