Progress Dialog is an extension of the Alert Dialog that we learned about in the previous post, This Progress Dialog can display progress animation like a small wheel or a bar that shows with how many percent complete of the progress. This dialog can have buttons too for example a Cancel button to cancel a download progress. Or when your application will save something or load something a Cancel button would work for that too.
Lets create a quick Progress Dialog.
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "Title", "Loading!", true);
Here we simply create a ProgressDialog and we tell it to show and inside the parameters we first got the Contect of the app, Then the Title, Then we got the text to display and at last we have an boolean that tells if the progress is indeterminate or not.
This is only if you want to show a progress bar like how many percent of the progress is left / done.. Only negative with this type of loading in my point of view is that the user that uses the app has no idea for how long it will load or what is going on. But thats only in my point of view, This simple dialog is perfect for some occasions.
Here is a picture on how this code above would look in an Android app:
Lets now make a Progress Dialog with a progress bar!
If we instead also want to tell the user how long it's loaded or how far the app gone in the loading (Loading is just an example you can use the Progress Dialog to whatever you want). Lets take a look at how we would make a Progress Dialog with a progress bar!
First we create the Progress Dialog:
After we did that we need to initiate it and also put our contect in the parameters:
myDialog = new ProgressDialog(MyActivity.this);
Now we set the style of our progress dialog(Lets go with default):
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);We can also add the rest of the properties like title and message:
myDialog.setTitle("Title"); myDialog.setMessage("This is my progress bar:");
And when you finished customising your Dialog you can finaly call show():
One problem is happening if we would run the app now.. The progress bar would be empty! We don't want an empty progress bar thats just not cool..
There is two ways to increment the percent on the progress bar.. We could use setProgress(int) to set the total percentage at once or we could use incrementProgressBy(int) to add the value inside the parameters to the total progress. And i think thats the way we will do for our dialog!
Add this peace of code after the show():
Now we should finaly have a half filled progress bar if we would run our app.. It should look something like this: