SoFunction
Updated on 2025-04-10

Android ProgressDialog usage summary

ProgressDialog inherits from AlertDialog, and AlertDialog inherits from Dialog, implementing the DialogInterface interface.

There are two ways to create ProgressDialog, one is new Dialog, and the other is to call Dialog's static method ().

// Method 1: new Dialogfinal ProgressDialog dialog = new ProgressDialog(this); 
(); 
// Method 2: Create and display it using static method. This progress bar can only be a circular bar. Set the title and Message prompt contentProgressDialog dialog2 = (this, "hint", "Logining"); 
// Method 3 Create and display using static method. This progress bar can only be a circular bar. Here, the last parameter boolean indeterminate setting is unclear.ProgressDialog dialog3 = ProgressDialog 
.show(this, "hint", "Logining", false); 
// Method 4 Create and display using static method. This progress bar can only be a circular bar. Here, the last parameter boolean cancelable sets whether the progress bar can be cancelled.ProgressDialog dialog4 = (this, "hint", "Logining", 
false, true); 
// Method 5 Create and display using static method. This progress bar can only be a circular bar. The last parameter here// cancelListener is used to listen for the progress bar being canceledProgressDialog dialog5 = (this, "hint", "Logining", true, 
true, cancelListener); 

In the fifth method, a cancelListener is required, and the code is as follows;

private OnCancelListener cancelListener = new OnCancelListener() { 
@Override 
public void onCancel(DialogInterface dialog) { 
// TODO Auto-generated method stub 
(, "Progress bar cancelled", Toast.LENGTH_LONG) 
.show(); 
} 
}; 

There are two types of ProgressDialog, one is the circular unclear state, and the other is the horizontal progress bar state.

The first method: circular progress bar

final ProgressDialog dialog = new ProgressDialog(this); 
(ProgressDialog.STYLE_SPINNER);// Set the progress bar in the form of a circular rotation progress bar(true);// Set whether it can be cancelled by clicking the Back key(false);// Set whether to cancel the Dialog progress bar outside the click Dialog(.ic_launcher);// 
// Set the icon of the title prompt, the default is not available. If you don’t set the title, just setting the Icon will not display the icon.("hint"); 
// dismiss monitor(new () { 
@Override 
public void onDismiss(DialogInterface dialog) { 
// TODO Auto-generated method stub 
} 
}); 
// Listen to the Key event is passed to the dialog(new () { 
@Override 
public boolean onKey(DialogInterface dialog, int keyCode, 
KeyEvent event) { 
// TODO Auto-generated method stub 
return false; 
} 
}); 
// Listen to cancel events(new () { 
@Override 
public void onCancel(DialogInterface dialog) { 
// TODO Auto-generated method stub 
} 
}); 
//Set the clickable buttons, up to three (by default)(DialogInterface.BUTTON_POSITIVE, "Sure", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
(DialogInterface.BUTTON_NEUTRAL, "neutral", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
("This is a circular progress bar"); 
(); 
new Thread(new Runnable() { 
@Override 
public void run() { 
// TODO Auto-generated method stub 
try { 
(5000); 
// The cancel and dismiss methods are essentially the same, both delete the Dialog from the screen. The only difference is// Calling the cancel method will callback. If registered, the dismiss method will not be returned.(); 
// (); 
} catch (InterruptedException e) { 
// TODO Auto-generated catch block 
(); 
} 
} 
}).start(); 

Among them, the background operation is simulated through (5000).

The cancel and dismiss methods are essentially the same, both of which delete the Dialog from the screen. The only difference is: calling the cancel method will callback. If registered, the dismiss method will not be dropped back.

The second method: horizontal progress bar

// The progress bar has a second-level progress bar, so I won't demonstrate it herefinal ProgressDialog dialog = new ProgressDialog(this); 
(ProgressDialog.STYLE_HORIZONTAL);// Set the horizontal progress bar(true);// Set whether it can be cancelled by clicking the Back key(false);// Set whether to cancel the Dialog progress bar outside the click Dialog(.ic_launcher);// Set the title icon for prompts, no default("hint"); 
(100); 
(DialogInterface.BUTTON_POSITIVE, "Sure", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
(DialogInterface.BUTTON_NEUTRAL, "neutral", 
new () { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} 
}); 
("This is a horizontal progress bar"); 
(); 
new Thread(new Runnable() { 
@Override 
public void run() { 
// TODO Auto-generated method stub 
int i = 0; 
while (i < 100) { 
try { 
(200); 
// Update the progress of the progress bar, you can update the progress bar in the child thread(1); 
// (10)// Level 2 progress bar update methodi++; 
} catch (Exception e) { 
// TODO: handle exception 
} 
} 
// Delete Dialog when the progress bar is finished(); 
} 
}).start(); 

I will introduce the relevant knowledge about the summary of Android ProgressDialog usage here. It will continue to be updated in the future. Please continue to pay attention to my website, thank you!