Apply gradient to Android View

Images have loat of memory than using colur. We also need different size images for different resolutions, so in Android we can use gradient that shade the color.

We can apply gradient to different Views in Android like EditText, TextView or Button etc.
It will be take less memory.

In your Android project add new xml filed in drawable folder.

 

eclipse-create-new-xml

I name this new xml to “rect_view”, and add some code for gradient.

This file will look like this

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
        android:startColor="#fff000"
        android:endColor="#fff0ff"
        android:angle="45"/>

We can use rounded corners of our View, look the complete code below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:startColor="#fff000"
        android:endColor="#fff0ff"
        android:angle="45"/>
    <!-- angle should be 0, 45, 90, 135, 180,.... -->
    
    <padding
        android:left="05dp"
        android:right="05dp"
        android:top="05dp"
        android:bottom="05dp"/>
    <!-- pading is used to make some distance from border of View -->
   
    <corners android:radius="10dp"/>
    <!-- this tag for make corners rounded -->
   
</shape>

we can change our desire colors, angle and conrner.

Finaly apply this file as background to any View like Button.

[sociallocker]

<Button
            android:id="@+id/btnGradient"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rect_view" />

[/sociallocker]

We can assign this shape to more than one different views.

Our output will like this.

android-xml-gradient-button

Leave a comment

Your email address will not be published. Required fields are marked *