Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add gradient effect to background color of TextView in a ListView?

In reference to these questions :

Adding gradient effect to TextView in a ListView generates NPE

and

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 background? Should I make a CustomListAdapter?

like image 852
Kazekage Gaara Avatar asked Jan 16 '13 17:01

Kazekage Gaara


People also ask

How do I add background color to TextView?

To change the background color of TextView widget, set the background attribute with required Color value. You can specify color in rgb , argb , rrggbb , or aarrggbb formats. The background color is applied along the width and height of the TextView.

How do I add color to a gradient in XML?

To create a gradient color we need to create a . xml file in the drawable folder. So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable. xml file.

How do you add color to a gradient?

Select the Gradient tool in the toolbar. In the selected artwork you'll see the gradient annotator, which shows the gradient slider and the color stops. Double-click a color stop on the artwork to edit the color, drag the color stops, click beneath the gradient slider to add new color stops, and more.


1 Answers

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"         /> ... 
like image 62
Booger Avatar answered Sep 19 '22 17:09

Booger