draw a rectangle shape in android

In this article, we will draw a rectangle shape using XML?

Create a solid rectangle shape as a resource –

First, we have to create a new XML file inside the resource->drawable folder in your android studio then save it as rectangle.xml.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#ffffffff" />
</shape>

Output –

rectangle in android
rectangle in android

Create a rectangle shape with a border as a resource –

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <stroke android:width="4dp" android:color="#000000" />
    <solid android:color="#ffffffff" />
</shape>

Output –

rectangle with border in android
rectangle with border in android

Create a solid rectangle shape with rounded corner –

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners android:radius="5dp" />
    <solid android:color="#ffffffff" />
</shape>

Output –

rectangle with rounded corner in android
rectangle with rounded corner in android

Create a solid rectangle shape with rounded corner and border –

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <stroke android:width="10dp" android:color="#000000" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="5dp" />
    <solid android:color="#ffffffff" />
</shape>

Output –

rectangle with rounded corner and border in android

Thank you 🙂

%d bloggers like this: