Updated 22 December 2016
In this blog,
I will show you how to make different shapes in android.
The shape is the XML Element which is defined in XML. and must be the root element.
The file location is :
res/drawable/your_shape.xml
The filename is used as the resource ID.
The values which define the type of shape,
"rectangle":Â
A rectangle that fills the containing View. This is the default shape.
"oval":Â
An oval shape that fits the dimensions of the containing View.
"line":Â
A horizontal line that spans the width of the containing View. This shape requires the  <stroke>  element to define the width of the line.
"ring":Â
A ring shape.
1 2 3 4 5 6 7 8 9 10 |
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/md_red_600" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> |
1 2 3 4 5 6 7 8 9 10 |
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/md_red_600" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> |
1 2 3 4 5 6 7 8 9 10 |
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring"> <solid android:color="@color/md_red_600" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> |
1 2 3 4 5 6 7 8 9 10 11 |
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <solid android:color="@color/md_red_600" /> <stroke android:width="1dp" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape> |
In Java:
R.drawable.
your_shape
In XML:@[package:]drawable/
your_shape
Create the file named circuler_shape.xml
12345678910 <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><solid android:color="@color/md_red_600" /><paddingandroid:bottom="5dp"android:left="5dp"android:right="5dp"android:top="5dp" /></shape>and this XML layout applies to a Textview:
1234567 <TextViewandroid:id="@+id/sale"android:layout_width="35dp"android:layout_height="35dp"android:background="@drawable/circuler_shape"android:gravity="center"android:text="SALE" />For programmatically:
12 TextView saleTv = (TextView)findViewByID(R.id.sale);tv.setBackgroundResource(R.id.circuler_shape);
Source:
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.