FileDocCategorySizeDatePackage
ProgressDialog.javaAPI DocAndroid 5.1 API12019Thu Mar 12 22:22:10 GMT 2015android.app

ProgressDialog

public class ProgressDialog extends AlertDialog

A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.

The dialog can be made cancelable on back key press.

The progress range is 0..10000.

Fields Summary
public static final int
STYLE_SPINNER
Creates a ProgressDialog with a circular, spinning progress bar. This is the default.
public static final int
STYLE_HORIZONTAL
Creates a ProgressDialog with a horizontal progress bar.
private android.widget.ProgressBar
mProgress
private android.widget.TextView
mMessageView
private int
mProgressStyle
private android.widget.TextView
mProgressNumber
private String
mProgressNumberFormat
private android.widget.TextView
mProgressPercent
private NumberFormat
mProgressPercentFormat
private int
mMax
private int
mProgressVal
private int
mSecondaryProgressVal
private int
mIncrementBy
private int
mIncrementSecondaryBy
private android.graphics.drawable.Drawable
mProgressDrawable
private android.graphics.drawable.Drawable
mIndeterminateDrawable
private CharSequence
mMessage
private boolean
mIndeterminate
private boolean
mHasStarted
private android.os.Handler
mViewUpdateHandler
Constructors Summary
public ProgressDialog(android.content.Context context)

    
       
        super(context);
        initFormats();
    
public ProgressDialog(android.content.Context context, int theme)

        super(context, theme);
        initFormats();
    
Methods Summary
public intgetMax()

        if (mProgress != null) {
            return mProgress.getMax();
        }
        return mMax;
    
public intgetProgress()

        if (mProgress != null) {
            return mProgress.getProgress();
        }
        return mProgressVal;
    
public intgetSecondaryProgress()

        if (mProgress != null) {
            return mProgress.getSecondaryProgress();
        }
        return mSecondaryProgressVal;
    
public voidincrementProgressBy(int diff)

        if (mProgress != null) {
            mProgress.incrementProgressBy(diff);
            onProgressChanged();
        } else {
            mIncrementBy += diff;
        }
    
public voidincrementSecondaryProgressBy(int diff)

        if (mProgress != null) {
            mProgress.incrementSecondaryProgressBy(diff);
            onProgressChanged();
        } else {
            mIncrementSecondaryBy += diff;
        }
    
private voidinitFormats()

        mProgressNumberFormat = "%1d/%2d";
        mProgressPercentFormat = NumberFormat.getPercentInstance();
        mProgressPercentFormat.setMaximumFractionDigits(0);
    
public booleanisIndeterminate()

        if (mProgress != null) {
            return mProgress.isIndeterminate();
        }
        return mIndeterminate;
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        LayoutInflater inflater = LayoutInflater.from(mContext);
        TypedArray a = mContext.obtainStyledAttributes(null,
                com.android.internal.R.styleable.AlertDialog,
                com.android.internal.R.attr.alertDialogStyle, 0);
        if (mProgressStyle == STYLE_HORIZONTAL) {
            
            /* Use a separate handler to update the text views as they
             * must be updated on the same thread that created them.
             */
            mViewUpdateHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    
                    /* Update the number and percent */
                    int progress = mProgress.getProgress();
                    int max = mProgress.getMax();
                    if (mProgressNumberFormat != null) {
                        String format = mProgressNumberFormat;
                        mProgressNumber.setText(String.format(format, progress, max));
                    } else {
                        mProgressNumber.setText("");
                    }
                    if (mProgressPercentFormat != null) {
                        double percent = (double) progress / (double) max;
                        SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                        tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
                                0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        mProgressPercent.setText(tmp);
                    } else {
                        mProgressPercent.setText("");
                    }
                }
            };
            View view = inflater.inflate(a.getResourceId(
                    com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout,
                    R.layout.alert_dialog_progress), null);
            mProgress = (ProgressBar) view.findViewById(R.id.progress);
            mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
            mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
            setView(view);
        } else {
            View view = inflater.inflate(a.getResourceId(
                    com.android.internal.R.styleable.AlertDialog_progressLayout,
                    R.layout.progress_dialog), null);
            mProgress = (ProgressBar) view.findViewById(R.id.progress);
            mMessageView = (TextView) view.findViewById(R.id.message);
            setView(view);
        }
        a.recycle();
        if (mMax > 0) {
            setMax(mMax);
        }
        if (mProgressVal > 0) {
            setProgress(mProgressVal);
        }
        if (mSecondaryProgressVal > 0) {
            setSecondaryProgress(mSecondaryProgressVal);
        }
        if (mIncrementBy > 0) {
            incrementProgressBy(mIncrementBy);
        }
        if (mIncrementSecondaryBy > 0) {
            incrementSecondaryProgressBy(mIncrementSecondaryBy);
        }
        if (mProgressDrawable != null) {
            setProgressDrawable(mProgressDrawable);
        }
        if (mIndeterminateDrawable != null) {
            setIndeterminateDrawable(mIndeterminateDrawable);
        }
        if (mMessage != null) {
            setMessage(mMessage);
        }
        setIndeterminate(mIndeterminate);
        onProgressChanged();
        super.onCreate(savedInstanceState);
    
private voidonProgressChanged()

        if (mProgressStyle == STYLE_HORIZONTAL) {
            if (mViewUpdateHandler != null && !mViewUpdateHandler.hasMessages(0)) {
                mViewUpdateHandler.sendEmptyMessage(0);
            }
        }
    
public voidonStart()

        super.onStart();
        mHasStarted = true;
    
protected voidonStop()

        super.onStop();
        mHasStarted = false;
    
public voidsetIndeterminate(boolean indeterminate)

        if (mProgress != null) {
            mProgress.setIndeterminate(indeterminate);
        } else {
            mIndeterminate = indeterminate;
        }
    
public voidsetIndeterminateDrawable(android.graphics.drawable.Drawable d)

        if (mProgress != null) {
            mProgress.setIndeterminateDrawable(d);
        } else {
            mIndeterminateDrawable = d;
        }
    
public voidsetMax(int max)

        if (mProgress != null) {
            mProgress.setMax(max);
            onProgressChanged();
        } else {
            mMax = max;
        }
    
public voidsetMessage(java.lang.CharSequence message)

        if (mProgress != null) {
            if (mProgressStyle == STYLE_HORIZONTAL) {
                super.setMessage(message);
            } else {
                mMessageView.setText(message);
            }
        } else {
            mMessage = message;
        }
    
public voidsetProgress(int value)

        if (mHasStarted) {
            mProgress.setProgress(value);
            onProgressChanged();
        } else {
            mProgressVal = value;
        }
    
public voidsetProgressDrawable(android.graphics.drawable.Drawable d)

        if (mProgress != null) {
            mProgress.setProgressDrawable(d);
        } else {
            mProgressDrawable = d;
        }
    
public voidsetProgressNumberFormat(java.lang.String format)
Change the format of the small text showing current and maximum units of progress. The default is "%1d/%2d". Should not be called during the number is progressing.

param
format A string passed to {@link String#format String.format()}; use "%1d" for the current number and "%2d" for the maximum. If null, nothing will be shown.

        mProgressNumberFormat = format;
        onProgressChanged();
    
public voidsetProgressPercentFormat(java.text.NumberFormat format)
Change the format of the small text showing the percentage of progress. The default is {@link NumberFormat#getPercentInstance() NumberFormat.getPercentageInstnace().} Should not be called during the number is progressing.

param
format An instance of a {@link NumberFormat} to generate the percentage text. If null, nothing will be shown.

        mProgressPercentFormat = format;
        onProgressChanged();
    
public voidsetProgressStyle(int style)

        mProgressStyle = style;
    
public voidsetSecondaryProgress(int secondaryProgress)

        if (mProgress != null) {
            mProgress.setSecondaryProgress(secondaryProgress);
            onProgressChanged();
        } else {
            mSecondaryProgressVal = secondaryProgress;
        }
    
public static android.app.ProgressDialogshow(android.content.Context context, java.lang.CharSequence title, java.lang.CharSequence message)

        return show(context, title, message, false);
    
public static android.app.ProgressDialogshow(android.content.Context context, java.lang.CharSequence title, java.lang.CharSequence message, boolean indeterminate)

        return show(context, title, message, indeterminate, false, null);
    
public static android.app.ProgressDialogshow(android.content.Context context, java.lang.CharSequence title, java.lang.CharSequence message, boolean indeterminate, boolean cancelable)

        return show(context, title, message, indeterminate, cancelable, null);
    
public static android.app.ProgressDialogshow(android.content.Context context, java.lang.CharSequence title, java.lang.CharSequence message, boolean indeterminate, boolean cancelable, OnCancelListener cancelListener)

        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setIndeterminate(indeterminate);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;