Tutorial details

Android beginner tutorial: Part 45 - ProgressDialog with a progress bar | App Code for Sale | Preview

Android beginner tutorial: Part 45 - ProgressDialog with a progress bar | Android Tutorial

How to display and handle a progress bar inside a ProgressDialog

Overview PAGE TOP

In this tutorial we will learn how to display and handle a progress bar inside a ProgressDialog.

A progress bar inside a ProgressDialog is the same progress bar we've talked about in one of my previous tutorials. It works the same way - it has the same methods and it also needs a handler.

First, let's declare our ProgressDialog and our Handler. In the Handler's handleMessage() function we increase the value of the dialog by 1 using incrementProgressBy() method:

public ProgressDialog myDialog;
public Handler handler = new Handler(){
@Override
public void handleMessage(Message message){
super.handleMessage(message);
myDialog.incrementProgressBy(1);
}
};

In the onCreate() function we do the same things we did in the previous tutorial. However, the code in the onClick() function of the click listener will be different.

Set myDialog to a new ProgressDialog object, set its title and message using setTitle() and setMessage() methods. Set the progress style to a progress bar using setProgressStyle() method. Use ProgressDialog.STYLE_HORIZONTAL static value as the parameter. Then set the current progress and maximum progress to 0 and 100 using setProgress() and setMax() methods. Use setCancelable(false) to make it impossible to dismiss the dialog using the back button. Finally, show() the dialog.

Then create a new Thread and start() it. Inside the thread, add a Runable, which has a run() method. Inside run() we add a while() method that executes the loop as long as getProgress() value of the dialog is lower than getMax() value. Inside the while loop we tell the Thread to sleep for 100 milliseconds and then send a message to our handler. After the loop, check if current progress equals maximum progress and dismiss the ProgressDialog if so.

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myDialog = new ProgressDialog(MainActivity.this, 0);
myDialog.setTitle("Loading...");
myDialog.setMessage("Please wait!");
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myDialog.setProgress(0);
myDialog.setMax(100);
myDialog.setCancelable(false);
myDialog.show();
new Thread(new Runnable(){
@Override
public void run(){
while(myDialog.getProgress()<myDialog.getMax()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
handler.sendMessage(handler.obtainMessage());
}
if(myDialog.getProgress()==myDialog.getMax()){
myDialog.dismiss();
}
}
}).start();
}
});

Full code so far:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{

public ProgressDialog myDialog;
public Handler handler = new Handler(){
@Override
public void handleMessage(Message message){
super.handleMessage(message);
myDialog.incrementProgressBy(1);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.testButton);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
myDialog = new ProgressDialog(MainActivity.this, 0);
myDialog.setTitle("Loading...");
myDialog.setMessage("Please wait!");
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myDialog.setProgress(0);
myDialog.setMax(100);
myDialog.setCancelable(false);
myDialog.show();
new Thread(new Runnable(){
@Override
public void run(){
while(myDialog.getProgress()<myDialog.getMax()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
handler.sendMessage(handler.obtainMessage());
}
if(myDialog.getProgress()==myDialog.getMax()){
myDialog.dismiss();
}
}
}).start();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

The results look like this:

mon48.png

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-45.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.