Layer-list drawable is not rendered fully from time to time

159 views Asked by At

I have encoutered problem where my layer-list drawable is not rendered fully from time to time.

In my activity's onCreate I have programatically created few TextViews, in onResume I am calling web service and after getting response I am updating this TextViews accordingly.

   TextView tv = getView().findViewWithTag(tag);

    if ((int) wt.getCustomerId() == Const.CUSTOMER_xxx) {

        tv.setBackgroundResource(R.drawable.tile_sended_xxx);

    } else {

        switch ((int) wt.getActivityId()) {

            case Const.ACTIVITY_WALK:
                tv.setBackgroundResource(R.drawable.tile_sended_walk);
                break;
            case Const.ACTIVITY_AUTO:
                tv.setBackgroundResource(R.drawable.tile_sended_auto);
                break;
            default:
                tv.setBackgroundResource(R.drawable.tile_sended_work);
                break;
        }
    }

And one of my drawables (blueck background, coffee icon):

<item>
    <shape
        android:padding="0dp"
        android:shape="rectangle">

        <solid android:color="@color/tile_sended_xxx"/>

        <corners
            android:bottomLeftRadius="4dp"
            android:bottomRightRadius="4dp"
            android:topLeftRadius="4dp"
            android:topRightRadius="4dp"/>

    </shape>

</item>

<item
    android:drawable="@drawable/coffee"
    android:gravity="center">
</item>

The problem is that from time to time only backgroud (yellow/blue) is rendered but icon is not visible.

It happens in different emulators, it happens on real device.

I have no idea how to debug/solve that. Any help will be apprecated.

I am adding also some screens to show that Android Studio displays it OK.

enter image description here

enter image description here

1

There are 1 answers

0
PiotrW On

Solved, it occured that in some other place I was setting drawables tint color, i.e. to yellow, and that change is saved to resources for later use. Later when layer-list was used then I had yellow (instead of black) drawable on yellow background.

So instead of changing "global" resource:

Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.walk);
d.setTint(getActivity().getColor(R.color.tile_sended));

mutate() should be used to make "local" resource copy:

Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.walk).mutate();
d.setTint(getActivity().getColor(R.color.tile_sended));