Lesson 67. Dialogues. ProgressDialog
Android Lessons

Lesson 67. Dialogues. ProgressDialog

In this lesson:

– we work with ProgressDialog

ProgressDialog allows you to show the user that any operation is underway and to wait. It is of two types: simply rotating a circle and a bar that shows the percentage of performance. Let’s create an application that will show both types.

Let’s create a project:

Project name: P0671_ProgressDialog
Build Target: Android 2.3.3
Application name: ProgressDialog
Package name: ru.startandroid.develop.p0671progressdialog
Create Activity: MainActivity

IN strings.xml write down the texts:




Two buttons: one shows the dialog with the block circle (ProgressDialog by default), the other – with the horizontal bar indicator


package ru.startandroid.develop.p0671progressdialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

public class MainActivity extends Activity {

  ProgressDialog pd;
  Handler h;

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {


  public void onclick(View v) {
    switch (v.getId()) {
    case R.id.btnDefault:
      pd = new ProgressDialog(this);
      // добавляем кнопку
      pd.setButton(Dialog.BUTTON_POSITIVE, "OK", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
    case R.id.btnHoriz:
      pd = new ProgressDialog(this);
      // меняем стиль на индикатор
      // устанавливаем максимум
      // включаем анимацию ожидания
      h = new Handler() {
        public void handleMessage(Message msg) {
          // выключаем анимацию ожидания
          if (pd.getProgress() < pd.getMax()) {
            // увеличиваем значения индикаторов
            h.sendEmptyMessageDelayed(0, 100);
          } else {
      h.sendEmptyMessageDelayed(0, 2000);

For the first dialogue everything is almost as usual. We first configure the header and message. Then we add a button. Here is a little different than with AlertDialog.Builder. We use the setButton method. We send the button type, text and handler to the input. Button type is determined by constants: BUTTON_POSITIVE, BUTTON_NEGATIVE, BUTTON_NEUTRAL. I don't write anything in the handler, I don't need it now. Next we show the dialog using the show method.

For the second dialogue, we indicate that it will be an indicator. This is done using the setProgressStyle method. Next we set the title and message. We do not add a button, but if suddenly it will be needed, it is done exactly like the first dialogue. The setMax method sets the maximum indicator fill value. I chose the value 2148 by chance. The setIndeterminate method includes an animation of an indicator that simulates expectations. The show method shows dialogue.

Then again we have to use an unfamiliar Handler. It is not necessary to understand its effect now, we will pass it later. Now I'm just going to say in words what he's doing. The handler waits for 2 seconds (just a simulation, such as a server connection), disables the setIndeterminate animation, then each 100 milliseconds increments the incrementProgressBy and incrementSecondaryProgressBy until the getPress is maximal , in our case = 2148). After that the dialog is closed (dismiss).

You could see an example of an additional indicator on YouTube. When you watch a video, the main indicator is filled in from the bottom as you play, and another dimmed one to the right, showing how much video has been cached. Of course, if you don't need it, you just don't fill it. Only the primary will be displayed.

We'll save everything and run it.

Let's call the first dialogue

The endless spin of the circle shows that the process is going. But how much is left is unclear. It is not always possible to predict the remaining amount or to show the percentage of task completion.

We close the first dialogue and open the second. It will hang for two seconds with an animation of waiting

And then the indicator (primary and secondary) will populate.

When the main indicator is full, the dialog will close.

I think the dialogs can be closed. We have covered them in great detail. The fact that while incomprehensible Handler is nothing to worry about, in a few lessons we will take up it. If suddenly something remains unclear, velcom to the forum, we will understand)

In the next lesson:

- get acquainted with Parcel

Discuss in the forum [22 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *