ProgressDialog - how to remove the numbers
Asked Answered
T

5

22

I was following the progress dialog example in the ApiDemos. all went great except for one thing - I want to remove the numbers that appear underneath the bar (those running numbers that run from 0 to .getMax().

couldn't find how to do it.

anyone?

Ori

Terrarium answered 9/5, 2010 at 14:17 Comment(1)
Here is another example with code sample https://mcmap.net/q/586843/-how-to-remove-progress-bar-number-at-the-bottom-part-of-the-progress-barKnot
R
4

Just looked through ProgressDialog.java, there's no official way.

The choices are:

  • Subclass it, access mProgressNumber via reflection to .setVisibility(View.GONE);

  • Write your own implementation.

Roommate answered 9/5, 2010 at 16:15 Comment(0)
H
67

With api 11, we can do it by calling:

progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);
Headstream answered 26/6, 2011 at 1:40 Comment(0)
A
9

Use this code for Android < API Level 11. It uses reflection to set the visbility to GONE:

public class CustomProgressDialog extends ProgressDialog {

public CustomProgressDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        Method method = TextView.class.getMethod("setVisibility",
                Integer.TYPE);

        Field[] fields = this.getClass().getSuperclass()
                .getDeclaredFields();

        for (Field field : fields) {
            if (field.getName().equalsIgnoreCase("mProgressNumber")) {
                field.setAccessible(true);
                TextView textView = (TextView) field.get(this);
                method.invoke(textView, View.GONE);
            }
        }
    } catch (Exception e) {
        Log.e(TAG,
                "Failed to invoke the progressDialog method 'setVisibility' and set 'mProgressNumber' to GONE.",
                e);
    }
}
}
Ammoniate answered 26/7, 2012 at 13:9 Comment(1)
I used your code to build my own that can also hide the percentage. thanksGranlund
L
5

For completeness, I use the answer of Martin to build my class:

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CustomProgressDialog extends ProgressDialog {

    private int progressPercentVisibility = View.VISIBLE;
    private int progressNumberVisibility = View.VISIBLE;

    public CustomProgressDialog(Context context, int progressPercentVisibility, int progressNumberVisibility) {
        super(context);

        this.progressPercentVisibility = progressPercentVisibility;
        this.progressNumberVisibility = progressNumberVisibility;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setFieldVisibility("mProgressPercent", progressPercentVisibility);
        setFieldVisibility("mProgressNumber", progressNumberVisibility);
    }

    private void setFieldVisibility(String fieldName, int visibility) {
        try {
            Method method = TextView.class.getMethod("setVisibility", Integer.TYPE);

            Field[] fields = this.getClass().getSuperclass()
                    .getDeclaredFields();

            for (Field field : fields) {
                if (field.getName().equalsIgnoreCase(fieldName)) {
                    field.setAccessible(true);
                    TextView textView = (TextView) field.get(this);
                    method.invoke(textView, visibility);
                }
            }
        } catch (Exception e) {
        }
    }
}

With this you can also hide the percentage.

Lamee answered 5/3, 2013 at 11:0 Comment(0)
R
4

Just looked through ProgressDialog.java, there's no official way.

The choices are:

  • Subclass it, access mProgressNumber via reflection to .setVisibility(View.GONE);

  • Write your own implementation.

Roommate answered 9/5, 2010 at 16:15 Comment(0)
L
1

I took @MartinVonMartinsgrün's answer and modified it a little bit. This solution simply takes advantage of the API call in Honeycomb+, whereas it uses reflection to accomplish the same goal for Gingerbread and before.

ProgressDialog dialog;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    dialog = new ProgressDialog(getContext());
    dialog.setProgressNumberFormat(null);
} else {
    dialog = new ProgressDialog(getContext()) {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            try {
                Field field = ProgressDialog.class.getDeclaredField("mProgressNumber");

                field.setAccessible(true);
                TextView textView = (TextView)field.get(this);
                field.setAccessible(false);

                textView.setVisibility(View.GONE);
            } catch (Exception e) {
                // Ignore the exception ... We'll just let the dialog show the bytes. It's not ideal but it works.
                Log.w("ProgressDialog", "Failed to hide the progress number via reflection.", e);
            }
        }
    };
}

// Further configure the dialog ...

dialog.show();
Lecture answered 9/12, 2016 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.