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 1:
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>
Solution 2:
I used the following for a drawable image with a circular background.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/colorAccent"/>
</shape>
</item>
<item
android:drawable="@drawable/ic_select"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"/>
</layer-list>
Here is what it looks like
Hope that helps someone out.
Solution 3:
This is a circle shape with icon inside:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ok_icon"/>
<item>
<shape
android:shape="oval">
<solid android:color="@color/transparent"/>
<stroke android:width="2dp" android:color="@color/button_grey"/>
</shape>
</item>
</layer-list>
Solution 4:
This is a corner image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/img_main_blue"
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<item>
<shape
android:padding="10dp"
android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="5dp"
android:color="@color/white" />
</shape>
</item>
</layer-list>