Create a Gradient in Android XML Layout


2012-02-12

Android provides an easy way to create a three-color gradient using an xml file. Simply create an xml file in your drawable folder like this:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient 
android:startColor="#ff8833" 
android:endColor="#ff5533"
android:angle="270"
/>
<corners android:radius="0dp" /> 
</shape>

Then reference it by its file name (it would be gradient.xml, here) as the background of the view you want to use to show it. Here it is as the background of a TextView:

<TextView
android:id="@+id/widget43"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal" 
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textColor="#ffffff"
android:gravity="center"
android:background="@drawable/gradient"
android:textSize="30dp"
android:text="Gradient"
>
</TextView>