Add a background image to shape in XML Android

android:shape=ring
android:shape drawable example
android draw shape
android set background drawable xml programmatically
polygon shape in android xml
android layer-list text
layer-list android
android:shape size

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>

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>

How to set background image to shape?, shape.xml Now set the shape for a layout, then add the image that you want in android:background="@drawable/shape"> <ImageView  How to add a image to drawable xml in android. height="120dp" /> </shape> now, I have to add a image in drawable folder. please help me where to change the code

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.

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  The background attribute supports images but it also supports shapes defined in XML. The XML files are also drawables, so sit in the res/drawable folder. Unlike images it is not necessary to produce a version for all screen densities. To add a shape drawable background for the ImageButtons begin by creating a new shape XML file.

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>

Drawables overview, To use an image resource, add your file to the res/drawable/ For example, you can use a ShapeDrawable object to set the background of a view by You can also define primitive drawable shapes using XML resources. In Android, you can use android.widget.ImageView class to display an image file. Image file is easy to use but hard to master, because of the various screen and dpi in Android devices. Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources

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>

Drawable resources, Add programs · Attributes · Guidelines for app developers · Guidelines for TV providers Nine-Patch File: A PNG file with stretchable regions to allow image resizing This is useful when a View needs a background drawable that is Creates a ScaleDrawable; Shape Drawable: An XML file that defines a  Create circular shape in drawable. Give gradient to the shape, and implement it in the layout..

I used the following for a drawable image with a border.
First make a .xml file with this code in drawable 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"/>

Second make a view .xml file in layout folder and call the above .xml file with 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

5.1: Drawables, styles, and themes · GitBook, Android provides classes and resources to help you include rich images in your In the above example the height and width are set to wrap_content , which means the If you use a normal PNG image as the background image for the View , the A shape Drawable is a rectangle, oval, line, or ring that you define in XML. You can specify the type of a shape using android:shape XML attribute in the shape tag. If you don’t specify the shape, the default rectangle type is selected. Other available shapes are oval, line and ring. Here is an example: android:shape= “oval” 2.) Rounded corners. Since our shape is a rectangle, we can round rectangle’s corners.

About shape, state list, layer list and nine-patch drawables in Android, An XML file that defines a geometric shape, including colors and gradients. This can be a great way to make full-screen background images that Now when we insert these drawables into the activity layout, there is no  Sadly the Documentation on android.com does not cover the XML attributes of the Shape classes. I think I should use an ArcShape to draw a circle but there is no explanation on how to set the size, the color, or the angle needed to make a circle out of an Arc. The Android Dev documentation has been improved since then.

Android Shape Drawables Tutorial, That, of course, clutters your app with copies of the same image. You can specify the type of a shape using android:shape XML attribute in the shape tag. you have to set your angle to android:angle=”45” (note that the angle has to be a multiple of android:background=”@drawable/my_custom_circle” You can also define primitive drawable shapes using XML resources. For more information, see Shape drawable in Drawable resource types. NinePatch drawables. A NinePatchDrawable graphic is a stretchable bitmap image that you can use as the background of a view. Android automatically resizes the graphic to accommodate the contents of the view.

ImageButton Background with Shape Drawable, Setting the ImageButton background attribute to a shape XML file allows The images are also on the free Android Graphic Resources page. Android allows us to create shapes using shape drawable. The basic shapes supported in shape drawables are Line, Oval, Rectangle and Ring. This tutorial focuses on how to create circle shape. The drawable created using android shapes are scalable and highly maintainable compared to pre graphics bundled with APK. Here we use Oval. If you have

Comments
  • You need to extend and create a custom shaped class. Look into RoundedImageView for example. It creates a round imageview so whatever iamge you set as background to it, it will appear as rounded
  • Thanks,this did work however the edges are no longer rounded?
  • do you want Image with round edges?? because it seems your shape does not have any color
  • My shape did have a white color and rounded edges. I thought the rounded edges would remain if I removed the color and used an image instead. If this is not possible that is ok
  • so image with rounded corners , not possible?
  • It is possible but you need to programmatically round the corners of the image. see this stackoverflow.com/questions/2459916/…
  • Is there something to set the gravity of the image.
  • @SagarDevanga for the drawable in the code above or when you include the image in your layout?
  • Firstly, I tried to use height and width to set the internal icon to 24dp (its actual size). This resized perfectly on the Preview in AS but had no effect on the tablet. Therefore I used your method. I set the height and width of the shape to 40dp and then top, bottom, left and right of the icon item to 8dp each. (40-24)/2=8. However, the resulting image looks the right size but is blurry, which I suspect is due to some kind of re-sizing of the PNG. Any suggestions?
  • TLDR; how would you create your image above with a 40dp diameter circle and a 24dp icon without losing image quality?
  • @LukeAllison how are you adding the icon to you UI and how big is the image that you are placing in the oval?
  • more perfect for who want to show caret image and border