user3903998
user3903998

Reputation: 3

Simple gradient in Android

I'm kinda new to Android, and I'm looking to create a really simple gradient of 2 colors, from top to bottom, display it on the view, and maybe save it as an image. I really didn't find an answer that suits my needs, I'm really looking for the simplest and most straight-forward way. Thanks!

Upvotes: 0

Views: 144

Answers (2)

Richard Kamere
Richard Kamere

Reputation: 799

I will answer in two parts creating gradient and the displaying it.

To create gradient Create a new xml file in your drawable folder and name it whatever you want. In this case i will call it myGradient.xml. Open myGradient.xml file and paste the code below which will help create two color gradient. You can change the values of color to what you need.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="6px" android:right="4dp">

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="#d7009482"
            android:endColor="#ad1c4e9b"
            android:centerX="100%"
            android:centerY="150%"
            android:type="linear"
            android:angle="135"/>
    </shape>
</item>
</layer-list>

This will give you output below. Gradient with two colors

The second part will be to display this gradient in view. Open your view and set background to be the gradient.

android:background="@drawable/myGradient

Hope that will help you

Upvotes: 1

Michał Szydłowski
Michał Szydłowski

Reputation: 3419

I think the easiest way is to create a simple Shape template in XML, and then use it in any view you want, like this:

shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:type= "linear"
        android:startColor="#474946"
        android:endColor="#181818"
        android:angle="270"/>

</shape>

then in your view tag just add:

 android:background="@drawable/shape"

Upvotes: 1

Related Questions