Although I couldn't find any SDK version constraints for LayerDrawable
(added in API level 1), the one I am using as a background acts differently depending on the minSdkVersion: if it is 15 or lower, the background is completely black, if it is 19 or higher (KitKat<), the layers are displayed as expected.
I am replacing the first item in the layer list programatically. Here is how I use the LayerDrawable
class (although there shouldn't be any problem):
BitmapDrawable bitmapDrawable = (BitmapDrawable) resources.getDrawable(R.drawable.xyz1);
bitmapDrawable.setColorFilter(getColor(), PorterDuff.Mode.MULTIPLY);
bitmapDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
LayerDrawable bgDrwbl = (LayerDrawable) resources.getDrawable(R.drawable.mylayerdrawable);
bgDrwbl.setDrawableByLayerId(R.id.frameBitmap, bitmapDrawable);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(bgDrwbl);
} else {
v.setBackground(bgDrwbl);
}
Where mylayerdrawable looks like the following:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/frameBitmap"
android:left="@dimen/card_frame_width"
android:right="@dimen/card_frame_width"
android:top="@dimen/card_frame_width"
android:bottom="@dimen/card_frame_width" >
<bitmap
android:tileMode="repeat"/>
</item>
<item>
<shape android:shape="rectangle" >
<stroke
android:height="@dimen/card_frame_width"
android:width="@dimen/card_frame_width"
android:color="#000000" />
<corners android:radius="4dp" />
</shape>
</item>
<item
android:left="0.4dp"
android:right="0.4dp"
android:top="0.4dp"
android:bottom="0.4dp">
<shape android:shape="rectangle" >
<stroke
android:height="1.8dp"
android:width="1.8dp"
android:color="#EAEAEA" />
<corners android:radius="4dp" />
</shape>
</item>
While debugging I noticed the following missing members of bgDrwbl
in minSdkVersion=15 compared to 19:
LayerDrawable.mLayerState.mAutoMirrored
LayerDrawable.mLayoutDirection
(member of the base class: Drawable
)I don't know if any of them are crucial if someone uses LayerDrawable like me(?), but I neither got any warning nor find any suggestion on using LayerDrawable
's with different SDK versions. Do you have any hints?
I was too prejudiced with the LayerDrawable
, as I had too much trouble making it work previously... But it turned out the problem has nothing to do with them or the minSdkVersion either, but lies within the shapes defined in the second and third item in case of some devices.
The same problem is already answered here. In short:
So never leave shapes with border without color as some devices will paint it black!