Search code examples
androidandroid-xml

Add a background image to shape in XML Android


How do you add a background image to a shape? The code I tried below but no success:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
//here is where i need to set the image
<solid android:color="@drawable/button_image"/>
    <corners
     android:bottomRightRadius="5dp"
     android:bottomLeftRadius="5dp"
     android:topLeftRadius="5dp"
     android:topRightRadius="5dp"/>
 </shape>

Solution

  • Use following layerlist:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="rectangle" android:padding="10dp">
                <corners
                     android:bottomRightRadius="5dp"
                     android:bottomLeftRadius="5dp"
                     android:topLeftRadius="5dp"
                     android:topRightRadius="5dp"/>
             </shape>
       </item>
       <item android:drawable="@drawable/image_name_here" />
    </layer-list>