5

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).

Community
  • 1
  • 1
Geeks On Hugs
  • 1,591
  • 2
  • 18
  • 31

1 Answers1

6

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.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • OK, yes, this worked and neither could I tell you how or why. Or more correctly I couldn't tell you why alternatives did not work. It must be MAGIC! :) Thanks for all the help. – Geeks On Hugs Feb 18 '13 at 21:44
  • 2
    BTW, it's not the padding that was buggy so much as the GlobalLayout event. Before I tried this fix I experimented with padding outside of the global layout event and that worked fine on the Xoom. Before this fix came along I was going to figure out how to conditionally test for a Xoom and put in a hard coded padding amount for the Xoom and use the alternate processing for the rest. But this took care of everything. FWIW I tried params.gravity = Gravity.CENTER before I tried the relative layout params and that did NOT work. Thx again. – Geeks On Hugs Feb 18 '13 at 21:47