Is it possible to draw a shape in xml, and use a png as the background to that shape? I already have the shape (it's a square with rounded corners), and i would like to put a background to that square.
Asked
Active
Viewed 6.6k times
2 Answers
93
Yes you can use any shape file as background for any view. This sample create rounded background with white color and black border around the shape.
Sample :
rounded_corner.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<stroke
android:width="0.5dp"
android:color="@color/color_grey" />
<solid android:color="@color/color_white" />
</shape>
u can use this as,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:background="@drawable/rounded_corner"
android:orientation="vertical" >

Padma Kumar
- 19,893
- 17
- 73
- 130

Drup Desai
- 995
- 7
- 2
-
3instead of white color can we place any drawable i.e an image? – Padma Kumar Jul 03 '12 at 12:13
-
No you can not add any drawable in shape xml it only takes color. – Drup Desai Jul 03 '12 at 12:22
-
1OP wants .png image to be in background. – Padma Kumar Jul 03 '12 at 12:41
-
Yeah, finally i "cheated", because i have drawn the shape in Photoshop and added the background manually,but i would still like an idea for this.Thanks for the answer! – Dorin Rusu Jul 03 '12 at 12:45
-
Can you describe in brief. I am unable to understand what are you trying to archive. – Drup Desai Jul 03 '12 at 12:50
-
@DrupDesai OP knows how to make a shape in xml, but the question is specifically about a shape with a png background, instead of a color – Adam Mar 14 '13 at 07:09
-
1Can a shape's background be a reference? like -> android:color="?btn_background" – Sandra Aug 01 '14 at 15:26
1
//try this way this will help you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner"
android:padding="2dp"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yourdrawable />
</LinearLayout>

Padma Kumar
- 19,893
- 17
- 73
- 130
-
In my blank `Xamarin.Android` project, there's no folder called `drawable` under `Resources`. If i put the shape inside the `mipmap-anydpi-v26` folder, then i can access it and use it properly inside the emulator, but the app crashes on a real divice with the following error : `Binary XML file line #1: Error inflating class`. Any idea on why this might be happening? Read my post [here](https://stackoverflow.com/questions/55583311/xamarin-android-crashes-when-trying-to-load-style-from-resource) – Software Dev Apr 08 '19 at 23:55