How to use gradient in any component of Android?

Asked

Viewed 4,807 times

3

How to use the degrade effect on any component of Android? As for example the navigation bar , or a simple TextView?

  • 1

    You mean in the late Actionbar, current Toolbar?

  • I say in any component, for example a textview, I want it to start in one color and change to another , a certain degrade effect

  • Check out my answer if it helps you: http://answall.com/a/155735/35406

  • It did, but what if I was going to do a gradient on Toolbar , as it would be ?

2 answers

7


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

  • startColor: initial color
  • centerColor: center color
  • endColor: final color
  • angle: the angle at which it can be rotated, at which it can be defined as 0, 90, 180, 270.

There are other attributes described in the documentation.

Then file can be created in the directory drawable for example 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 finish, just set as background of some view, being a 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!"
    />
  • @The answer was clear or needs some more example?

  • Yes, thank you friend

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>

Assign your drawable to the background property of your component

    <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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.