How to use a shape drawable together with an image?

Surya Wijaya Madjid picture Surya Wijaya Madjid · Jun 7, 2011 · Viewed 31.3k times · Source

I have this difficulty to have both rounded corner and a background image in my LinearLayout.

I know I can achive the rounded corner by using the shape drawable XML, but if I put the shape drawable as the background of my LinearLayout with android:background=@drawable/rounded_corner then I cannot assign any image to be used as a background.

How can I get both a rounded corner with a background image in my LinearLayout? Any help would be appreciated. Thanks!

Answer

woodshy picture woodshy · Jun 7, 2011

You could use LayerDrawable, which could contain as many layers(shapes or images) as you need. You can create it either as resource or programmatically.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rounded_corners" android:id="@+id/rounded_corners"/>
    <item android:drawable="@drawable/additional_image" android:id="@+id/additional_image" />
</layer-list>