Add Android XML Background Image

How do you add a background image to a form? The code I tried below but was not successful:

<?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> 
+81
android android xml
Jan 08 '14 at 17:14
source share
5 answers

Use the following layerlist :

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/image_name_here" /> <item> <shape android:shape="rectangle" android:padding="10dp"> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp"/> </shape> </item> </layer-list> 
+116
Jan 08 '14 at 17:19
source share

I used the following for a drawn 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

enter image description here

Hope someone helps.

+117
Jul 24 '15 at 19:44
source share

This is a circle shape with an 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> 
+15
Dec 04 '14 at 9:47
source share

I used the following for a drawn picture with a frame.

First, create an .xml file with this code in a portable folder:

 <?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/orange"/> </shape> </item> <item android:top="2dp" android:bottom="2dp" android:left="2dp" android:right="2dp"> <shape android:shape="oval"> <solid android:color="@color/white"/> </shape> </item> <item android:drawable="@drawable/messages" //here messages is my image name, please give here your image name. android:bottom="15dp" android:left="15dp" android:right="15dp" android:top="15dp"/> 

Then create a file with the extension .xml in the layout folder and call the above .xml file in this way

 <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/merchant_circle" /> // here merchant_circle will be your first .xml file name 
+2
Dec 15 '15 at 12:15
source share

Here is another easiest way to get a custom shape for your image (Image View). It might be helpful for someone. This is just one line code.

First you need to add the dependency:

 dependencies { compile 'com.mafstech.libs:mafs-image-shape:1.0.4' } 

And then just write a line of code like this:

 Shaper.shape(context, R.drawable.your_original_image_which_will_be_displayed, R.drawable.shaped_image_your_original_image_will_get_this_images_shape, imageView, height, weight); 
+1
Jul 19 '17 at 15:25
source share



All Articles