How to Dynamically Change Android™ ListView Text Colors

Dynamically Change Android™ ListView Text Colors
The default Android ListView object is difficult to work with if creativity is one of your design factors. In my apps, I like to offer users the preference of choosing a light or dark color scheme as they desire. This requires programatically changing every object displayed on the screen on the fly once the user exits the preference screen. It's no problem changing the background color of a ListView object, but changing the TextColor is a whole other issue. There is no native hook to reach the TextView field used to display the ListView's text. There used to be in earlier versions of Android, but I'm writing for Froyo and higher. The android:setTextColor directive is no longer available in the ListView object. To further complicate matters, my ListView objects are displayed on a TabWidget. I have one ListView for each tab. For a 5-tabbed app, I have to change 5 ListViews.

Let's start this tutorial with a simple Linear Layout for the TabHost as our main.xml.

Notice the highlighted code at lines 21, 25, 29, 33 and 37. Setting the android:cacheColorHint to #00000000 overrides a common issue developers face when customizing the ListView background. By default, many Android widgets, ListView included, have a transparent background. This means you can see through the default window's background. ListView also enables fading edges by default. The fading edges technique is used throughout the system to indicate that a container can be scrolled. Fading edges is a gradient display rendered as a very dark gray to black fade. The fade effect is implemented using a combination of Canvas.saveLayerAlpha() and the Porter-Duff Destination Out blending mode. When we customize the background color, the blending optimzation leaves part of the dark gradient at the bottom of the screen. Scrolling the ListView changes all the TextView abjects back to their original color. To disable the optimization, simply use the transparent color #00000000 and you won't have any problems.

Next we're going to create a layout for the ListView rows. This file is called tab_list_layout.xml. Start this file by coping the simple_list_item_1 layout from the Android SDK.

In our layout file, we've created two TextView objects. One for the dark color scheme and one for the light. Name them accordingly (lines 7 and 17) and assign the desired colors to them (lines 11 and 21). The colors are defined in your colors.xml resource.

Next we're going to setup our adapter. Remember, the text color is driven by our application user preference. So first we have to retrieve the user preference:

Line 4 is the key to changing the colors dynamically. The variable list_text_id is set to the id number of appropriate TextView widget defined in tab_list_layout.xml depending on the user's color scheme preference. The list_text_id variable is passed to the ArrayAdapter as a parameter. Take note: if you are using a tabbed interface, you need to create a separate adapter for each tab on which you place a ListView widget. R.array.ListItems is the actual text you want to appear in the ListView rows defined in strings.xml.

Okay, so the setup is all complete, everything up till now will get you to the point where the colors will change when you first start your app. How do we code to change the text colors dynamically immediately after a user changes their preference?

When we instantiate the preferences activity, we start it to return a result. When the result is returned from the preference activity, we run the code to instantiate our ListView adapter(s). In my case, I've created a method called SetTabColors() which I call twice. The first time after I create the tabs in the onCreate mehtod, the second time whenever a user changes their preferences.


  • Android is a trademark of Google Inc.
  • The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. runs on the Genesis Framework

Genesis FrameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Check out the incredible features and the selection of designs. It's that simple—start using Genesis now!

Click here to download The Genesis Guide for Absolute Beginners (PDF - 1.4 MB)


  1. Iaudatkam says

    Hi, your post looks seems to be the one I’m looking for. I have been trying to find ways to switch between dark and light theme using preferenceActivity but so far without any success. I have an app with navigation drawer which has multiple listviews and gridviews. I’ll try your solution.

    But is there anyway I could get the code for the PreferenceActivity you’re using in this post? Thank you so much for this wonderful article.

  2. Ray says

    Simply brilliant~ I have been trying to find a dynamic way to change listview text color for a long time. A lot of tutorials were too complex and messy to follow, so I gave up on them, but yours only took me two times to read it before I figured out what’s happening. Now I got it working in my apps so the listview can change alongside selected theme colors.
    I have 2 suggestions to add for those who may run into the problems that I did.
    1. I get an error here: <TextView xmlns:android=";
    …as I deleted the xmlns part, even though errors went away, it didn't work, so after a quick search someone suggested to "Clean Project" (I use Eclipse), and after doing so it worked, the errors will go away and the code doesn't need any changes.
    2. The height of the textview creates a huge gap in the list, so I deleted the line android:minHeight="?android:attr/listPreferredItemHeight". It's still look a little spaced than usual, or maybe the vertical centering is not working correctly on mine.

    Thanks for the tutorial

  3. Phan says

    My list view record in Database and I want set color for 1 RECORD ( ONLY 1 ). How ??? Please tell me by e-mail. Thanks !!!
    I use Cursor and add cursor to SimpleCursorAdapter. Then, I add SimpleCursorAdapter to listView by code :

    Please tell me, thanks !!!
    ( Sorry, My English is BAD but I can read the your answer. Thank you !!! ).

Leave a Reply

Your email address will not be published. Required fields are marked *