0

I am trying to change text color of my Listview.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_multiple_choice, stg1);

I have used default XML to call list of data and now the requirement is to change text-color of this list-view. I know I can do with other custom classes but is there any other way that I can change text-color by using this class only.

I have searched lot and tried so many other solution but every one is suggesting to use custom class, but I don't want to use custom class.

Below is link which I have referred.

How to change the list view text color?

Android: Change text color in ListView for singleChoice

how to change the color of the text of the default ListView in android?

Android ListView Text Color

Change ListView's textcolor

How to Change List View Text color

Ans so many other link also but every one is suggesting to use custom class, take text-view and change the color of text-view but I don't want to use any other XML file or any custom class.

Even I don't know this is possible or not so please help me. Thanks in advance.

Community
  • 1
  • 1
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • Why don't you want to use a custom class? It gives you more flexibility.. using a custom class, this can be done in just a few lines of code.. – Amulya Khare Dec 13 '13 at 07:36
  • I know, but i am loading data from database inside this list-view and this is the very easiest way to load data from database with simple check box with multiple check. – InnocentKiller Dec 13 '13 at 07:38
  • There will be no difference between this and a custom class.. in terms of loading data from database and having multiple checkboxes.. – Amulya Khare Dec 13 '13 at 07:44
  • @AmulyaKhare, i tried with custom classes to load data from database inside listview with multiple checkbox but i did not get success. If you have any working example then please suggest or give a link. That will be a great help. – InnocentKiller Dec 13 '13 at 07:46
  • Also do you want to change the color of text box depending on position or change all of them to some particular color (say all text now in red)? – Amulya Khare Dec 13 '13 at 07:47
  • Change all with same color not depending on position or any other thing... – InnocentKiller Dec 13 '13 at 07:48
  • Then the answer is simpler.. just try my answer first and let me know. I will post in a bit – Amulya Khare Dec 13 '13 at 07:49
  • But if i use custom adapter class then i am not able to get all data in list-view. That's why i have used this. If you have any working code, example or any link then please suggest me that will be a big help. – InnocentKiller Dec 13 '13 at 07:52

4 Answers4

4

Step 1): Create an XML layout file custom_list_item_multiple_choice:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:textColor="#FF0000"
/>

Where, android:textColor="#FF0000" specifies your text color.

Step 2): Init the adapter like this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
 R.layout.custom_list_item_multiple_choice, stg1);

everything else remains the same.. everything should work fine.

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • Thanks a lot... One more question if i want to change text based on position then how can i do that. – InnocentKiller Dec 13 '13 at 08:07
  • Then you definitely need a custom class that extends `ArrayAdapter`. You simply override the `getView()` method. Thats all. Rest everything remains the same.. – Amulya Khare Dec 13 '13 at 08:16
  • Thanks for the answer. But i don't know how to use this custom class. can you give example please. – InnocentKiller Dec 13 '13 at 08:21
  • This is probably not the right place to post an example, but you could take a look at these similar answers that I have posted. Thats exactly what you need to do: http://stackoverflow.com/a/19657563/827110, http://stackoverflow.com/a/20416230/827110 – Amulya Khare Dec 13 '13 at 08:27
0

try

textView.setTextColor(Color.rgb(0,102, 51)); in inside your custom adapter textview

if you didn't use the custom adapter use ArrayAdapter<Spanned>() to make the custom color for the text.

Nambi
  • 11,944
  • 3
  • 37
  • 49
0

this may help you...

    ArrayList<String> arrayList = new ArrayList<String>();
    // add your String items to array list
    ArrayList<Spanned> spannedList = new ArrayList<Spanned>(
            arrayList.size());
    for (int i = 0, N = arrayList.size(); i < N; i++) {
        String s = arrayList.get(i);
        String html = "<font color=\"#0000FF\">" + s + "</font>"; // blue color
        Spanned spanned = Html.fromHtml(html);
        spannedList.add(spanned);
    }
    ArrayAdapter<Spanned> adapter = new ArrayAdapter<Spanned>(
            this, android.R.layout.simple_list_item_1, spannedList);

you can set text to any color using color code. (here I used blue 0000FF). for more color codes see here

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

You can use

Html.fromHtml()

Before using this you need to iterating the adapter value and store that value in one spanned variable like

Spanned out=Html.fromHtml("here you can use html tags like font color ,heading tag etc");
Devolus
  • 21,661
  • 13
  • 66
  • 113
Muthuraja
  • 551
  • 5
  • 13