Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources returns a Drawable of the wrong type

I have an app with three ProgressBars, each with its own custom progress Drawable. These Drawables are fairly straightforward- each is a LayerDrawable with a transparent background layer, a transparent secondary progress layer, and a solid color progress layer.

All three ProgressBars are displayed in the same layout.

The issue I started running into is that one of these ProgressBars is not displaying correctly- it simply does not display any progress. This only occurs on some devices (confirmed on a emulated Nexus One running 2.3.3 and a Galaxy SII running 4.1.2).

I put a breakpoint in onCreate, and discovered that the first two ProgressBars have their mProgressDrawable property correctly set to a LayerDrawable, but the third is set to a ColorDrawable.

Sure enough, the following code returns two LayerDrawables and one ColorDrawable:

    Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
    Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
    Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);

No matter where I move the third ProgressBar in the layout and in the code, or try to swap around the progressDrawable attributes, the one referencing my third XML Drawable displays no progress and gives me a ColorDrawable.

Interestingly, I discovered that simply creating a new XML file in my drawable folder fixes the problem. This leads me to believe that there is an issue with the way that Android is packaging or loading my resources, but I am having trouble figuring out how to identify and correct the root problem.

I also cannot reproduce the problem in a new application.

How can I continue tracking down the source of this problem?

progressDrawable XML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="@android:color/transparent" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/myapp_green" />
                <!-- The other two drawbles only change this color -->
            </shape>
        </clip>
    </item>

</layer-list>

colors.xml:

<resources>
    <color name="myapp_red">#dd514c</color>
    <color name="myapp_green">#5eb95e</color>
    <color name="myapp_blue">#0e90d2</color>
</resources>

Update from moving the progressDrawable attributes around

  • red, blue, green: 3rd is broken
  • red, green, green: 2nd and 3rd are broken
  • blue, green, red: 2nd is broken
  • blue, red, blue: all work
  • green, green, green: all work
  • green, green, red: all work
  • green, blue, red: all work
like image 643
Bryan Herbst Avatar asked Mar 01 '14 04:03

Bryan Herbst


1 Answers

If you change the order, from:

Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);

to for example:

Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);

is than still the greenDrawable the problem or still the 3rd position (blueDrawable)?

like image 158
lukas Avatar answered Sep 21 '22 13:09

lukas