42

In reference to these questions:

Adding gradient effect to TextView in a ListView generates NPE
How to change color and font on ListView

I would like to know how to go about setting the background of a TextView in a ListView with gradient effect?

In one of the questions above, I ended up having the gradient effect added to the text in the TextView. And after skimming through the second question, it seems I can add only fixed background colors.

How do I go about adding gradient to the background?

Should I make a CustomListAdapter?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108

3 Answers3

106

You just need to create a drawable resource (see an example below), and add it to the layout you created for your ListItem.

The drawable (in your res\drawable folder - name it whatever - listgrad.xml for ex) could look like:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
      android:startColor="@color/gradient_start"
      android:endColor="@color/gradient_end"
      android:angle="-270" /> 
</shape>

The you would add it to the layout for your list item (the layout.xml file you define for this) like this code snippet:

<TextView
        android:id="@+id/ranking_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_grad"
        />
...
Booger
  • 18,579
  • 7
  • 55
  • 72
  • 1
    thanks but i want only gradient for textview not backgroud. how can i do? – saigopi.me Aug 27 '17 at 04:28
  • You can't apply gradient to a TextView FWIW, @SaiGopiMe (I see your question is 3 years old, but better late then never). – Booger Dec 16 '20 at 12:56
  • 1
    @saigopi.me, you can use this Library to apply gradient on a textview https://github.com/veeyaarVR/SuperGradientTextView It worked for me. Thanks – Ali Raza Jan 04 '22 at 07:18
  • 1
    Yuck, why would you add a 3rd party lib, to handle such a simple thing? I don't want another dependency in my app. – Booger Jan 04 '22 at 15:57
10

Once you create a gradient you can apply it to pretty much anything let it be textView, layout or button.

To understand how to create and use a gradient refer this link.

To create a gradient you need to add it to the below directory

enter image description here

Code for gradient would be something like this -

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#ff2d9a59"
                android:centerColor="#ff42959a"
                android:endColor="#ff23729a"
                android:angle="135"/>
        </shape>
    </item>
</selector>
Ajit Singh
  • 2,436
  • 23
  • 27
2

Referred from here : How do I create a ListView with rounded corners in Android? (I have found it very useful.)

Add the following into a file (say gradient.xml) and then place it in (res/drawable/gradient.xml) directory.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient 
         android:startColor="#SomeGradientBeginColor"
         android:endColor="#SomeGradientEndColor" 
         android:angle="270"/> 

    <corners 
         android:bottomRightRadius="7dp" 
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp" 
         android:topRightRadius="7dp"/> 
</shape> 

Once you are done with creating this file,just set the background in one of the following ways:

Through Code: listView.setBackgroundResource(R.drawable.customshape);

Through XML,just add the following attribute to the container (ex: LinearLayout or to any fields):

android:background="@drawable/customshape"
Community
  • 1
  • 1
Rohan K
  • 872
  • 11
  • 18