SoFunction
Updated on 2025-04-11

android xml sample code to define gradient background

android xml defines gradient background

In Android, you can use XML to define gradient backgrounds. Here is a simple example showing how to use shape drawable to create a linear gradient background:

Create a file named gradient_background.xml in the res/drawable directory.

Add the following XML code to define the gradient background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro>
    <gradient
        android:startColor="#FF0000"
        android:endColor="#0000FF"
        android:angle="0" />
</shape>

This XML defines a horizontal gradient from red to blue. android:startColor is the start color of the gradient, android:endColor is the end color of the gradient, android:angle defines the direction of the gradient, 0 means from left to right, and 90 means from top to bottom.

Reference this background in your layout file or style:

&lt;LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_background"
    android:orientation="vertical"&gt;
    &lt;!-- Other views --&gt;
&lt;/LinearLayout&gt;

In this example, we set the gradient background to the background of LinearLayout. You can apply the background to any view or layout element as you want.

This is the end of this article about defining gradient backgrounds of Android xml. For more related contents of defining gradient backgrounds of Android xml, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!