How to use gradient on some Android component?

2

How do I use the degrade effect on some Android component? Such as the navigation bar, or a simple TextView ?

    
asked by anonymous 22.03.2017 / 12:14

2 answers

5

XML provides the tag <gradient> in which it can perform this feat. Basically you define an initial color, a central color (not mandatory) and a final color to accomplish such an effect. See below for the following attributes:

  • startColor : initial color
  • centerColor : central color
  • endColor : final color
  • angle : The angle at which it can be rotated, in which it can be set to 0 , 90 , 180 , 270 .

other attributes are described in the documentation.

Then you can create a file in the drawable directory with the name gradient.xml :

  

drawable / gradient.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <gradient 
            android:startColor="#6586F0"
            android:centerColor="#D6D6D6"
            android:endColor="#4B6CD6"
            android:angle="90"/>
</shape>

To end, just set the background of some view , being Button , TextView , Toolbar , LinearLayout , etc. See:

android:background="@drawable/gradient"

Application example:

<Button
    android:id="@+id/thebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient"
    android:text="Button Gradient!"
    />
    
22.03.2017 / 13:17
2

Create a gradient layout in res/drawable

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#faef4f"
        android:endColor="#01040a"
        android:angle="-90"/>
</shape>

Give the background property of your component its drawable

    <TextView
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="30dp"
            android:gravity="center_vertical"
            android:text="@string/empresa"
            android:textColor="#070701"
            android:maxHeight="5dp"
            android:textSize="18sp"
            android:layout_weight="1"
            android:typeface="monospace"
            android:textStyle="bold"
            android:background="@drawable/gradient">*
            android:layout_marginRight="30dp">
        </TextView>
    
22.03.2017 / 12:34