Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Created Android ProgressBar (Circle) Invisible On Xoom (Works Everywhere Else!)

The Problem

I'm having a weird issue that I can't figure out and need some other eyes to look at. I have dynamically created a ProgressBar view and it works on a variety of devices and AVDs except for my Motorola Xoom.


Screenshots

All screenshots have the device name above them and have been reduced around 50% from whatever it grabbed through the ADT except for the Nexus 10 which is 25% because of it's super high resolution. The red background for the ProgressBar is added for debug purposes to show that the view is there and visible. I added left and top padding to center it.

Screenshots below starting with the Xoom where it does not work and followed by Nexus 10 (AVD), Nexus 7, Galaxy Nexus and HTC Droid Incredible demonstrating it working everywhere else. After the shots I'll add my code.

Motorola Xoom Not Working

Progress bar not working! But note the view is visible (we see the red background).

Motorola Xoom

Working Fine On Other Devices

The circle progress is visible on all of these devices.

Simulated Samsung Nexus 10

Simulated Samsung Nexus 10

ASUS Nexus 7

ASUS Nexus 7

Samsung Galaxy Nexus

Samsung Galaxy Nexus

HTC Droid Incredible

HTC Droid Incredible


Now the code

This creates the parent view that contains it:

    RelativeLayout discoverListLayout = new RelativeLayout(mContext);
    discoverListLayout.setLayoutParams(params);
    discoverListLayout.addView(mDiscoverList);

mDiscoverList is a list view that is ultimately displayed. The ProgressBar is loaded on top of it until the list is loaded.

Now create and setup the progress bar. I setup the padding onGlobalLayout because otherwise the objects have 0 width and height.

    mDiscoverLoading = new ProgressBar(mContext);
    params.height = ViewPager.LayoutParams.WRAP_CONTENT;
    params.width = ViewPager.LayoutParams.WRAP_CONTENT;
    mDiscoverLoading.setLayoutParams(params);
    mDiscoverLoading.setIndeterminate(true);
    discoverListLayout.addView(mDiscoverLoading);
    ViewTreeObserver vto = mDiscoverLoading.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            if (mDiscoverList.getCount() > 0) {
                processLoadedList(true);
            } else {
                mDiscoverLoading.setBackgroundColor(getActivity().getResources().getColor(R.color.stitchoid_red));
                mDiscoverLoading.setPadding((getView().getWidth() / 2) - (mDiscoverLoading.getWidth() / 2),(getView().getHeight() / 2) - (mDiscoverLoading.getHeight()), 0, 0);
            }
            ViewTreeObserver obs = mDiscoverLoading.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
        }
    });

So again everything works like a champ except on the Motorola Xoom!? I would be grateful for any insights or suggestions to help solve this problem. Please let me know if I should provide any additional information.

Thank you very much!

*Addendum: A weird thing of note that is probably not very relevant but just in case: if I set the ProgressBar width to match_parent (and don't add width padding) it also works everywhere else but then on the Xoom it does show but is distorted as it's stretched to the width (xoom only).

like image 831
Geeks On Hugs Avatar asked Feb 18 '13 19:02

Geeks On Hugs


People also ask

How to change progress bar size in android?

You can use LayoutParams to change width and height to whatever you want. ProgressBar progressBar = new ProgressBar(teste. this, null, android. R.

Why do we add progress bar on a layout?

In Android, ProgressBar is used to display the status of work being done like analyzing status of work or downloading a file etc. In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal.

Is a user interface control which is used to Indicate the progress of an operation?

Progress bar is a user interface control that shows the progress of any operation.

How to create a circular ProgressBar in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Create a drawable resource file (circularprogressbar.xml) and add the following code Let's try to run your application.

What is Horhor horizontal progress bar in Android?

Horizontal progress bar is basically used to show downloading process dialogue because horizontal progress bar fill up slowly or fast as user requirement. It is apply with handler and thread So here is the complete step by step tutorial for Android horizontal Progress Bar example tutorial . Code for MainActivity.java file.

How do I add a progress bar to a XML file?

To add a progress bar to a layout (xml) file, you can use the <ProgressBar> element. By default, a progress bar is a spinning wheel (an indeterminate indicator). To change to a horizontal progress bar, apply the progress bar’s horizontal style.

What is the difference between progress and progress drawable in Android?

3. progress: progress is an attribute used in android to define the default progress value between 0 and max. It must be an integer value. Below we set the 100 max value and then set 50 default progress. 4. progressDrawable: progress drawable is an attribute used in Android to set the custom drawable for the progress mode.


1 Answers

I honestly don't know how or why it would make a difference.

But try using RelativeLayout.CENTER_IN_PARENT to get your ProgressBar into the center of your screen, rather than manually setting the top and left padding to be half the size of the screen.

So instead of:

mDiscoverLoading.setPadding((getView().getWidth() / 2) - (mDiscoverLoading.getWidth() / 2),(getView().getHeight() / 2) - (mDiscoverLoading.getHeight()), 0, 0);

use

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
mDiscoverLoading.setLayoutParams(lp); 

Note that there are several different "versions" of the LayoutParams object, you'll want to ensure that RelativeLayout.LayoutParams is the one you import since your parent is a RelativeLayout in this case.

I couldn't really tell you what is causing the padding to be buggy on the Xoom, but this is an alternate way that you can get the ProgressBar to the center, which seems to not be affected by whatever bug is causing the issue on the xoom.

like image 125
FoamyGuy Avatar answered Oct 26 '22 22:10

FoamyGuy