Rounded Rectangle with/without Background & Gradient XML in Android

While designing Android layouts we have to make custom XML based designs. In this article, I am going to share 3 types of XML layouts which will help you to drawable rounded rectangles in your Android Apps, and you can use them as a drawable anywhere in your project. Following are the XML code to create a rounded rectangle with/without background & gradient.

Video Tutorial

Rounded Rectangle without Background XML

Rounded Rectangle without Background XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#FF0000"/>
<corners
android:radius="25dp"/>
</shape>

Save it in your drawable folder by creating a new layout file and then you can assign it as a background using @drawable annotation in your project.

Rounded Rectangle With Background XML

You can modify the above XML layout to make this rectangle filled.

Rounded Rectangle With Background XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="#FF0000"/>
    <solid android:color="#5089fa"/>
    <corners
        android:radius="25dp"/>
</shape>

Rounded Rectangle With Gradient XML

We can modify the above XML layout to make this rectangle filled with a gradient (A combination of different colors).

Rounded Rectangle With Gradient XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#FF0000"/>
<solid android:color="#5089fa"/>
<gradient
android:startColor="#5089fa"
android:centerColor="#D6D6D6"
android:endColor="#ffcccb"
android:angle="0"/>
<corners
android:radius="25dp"/>
</shape>

That’s it. This is how you can create a rounded rectangle with background & gradient. Enjoy 🙂

For other helping, code snippets on Android layout designs have a look at our Articles List.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: