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

Regarding these issues:

Adding a gradient effect to a TextView in a ListView generates NPE

and

How to change color and font in ListView

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

In one of the questions above, I ended up adding a gradient effect to text in a TextView . And after going over the second question, it seems I can add only fixed background colors.

How do I add a gradient to the background? Should I do a CustomListAdapter ?

+22
android gradient android-textview
Jan 16 '13 at 17:52
source share
3 answers

You just need to create a resource with the ability to draw (see the example below) and add it to the layout created for your ListItem.

The selectable one (in the res \ drawable folder - name it whatever - listgrad.xml for ex) might look like this:

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

You would add it to the layout for your list item (the layout.xml file that you define for this), as this piece of code:

 <TextView android:id="@+id/ranking_order" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/list_grad" /> ... 
+59
Jan 16 '13 at 17:56
source share

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

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

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

enter image description here

The code for the gradient will 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> 
+4
Sep 09 '15 at 13:29
source share

Summary from here: How to create a ListView with rounded corners in Android? (I found this very useful.)

Add the following to the file (say gradient.xml) and then put it in the directory (res / drawable / gradient.xml).

 <?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 creating this file, just set the background in one of the following ways:

Via code: listView.setBackgroundResource(R.drawable.customshape);

Via XML, simply add the following attribute to the container (for example: LinearLayout or any fields):

 android:background="@drawable/customshape" 
+1
Jul 15 '14 at 2:21
source share



All Articles