Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text color of simple list item

I have an ListActivity and i am displaying one list with:

setListAdapter(new ArrayAdapter<String>(getApplicationContext(),                 android.R.layout.simple_list_item_single_choice, mStringList)); 

By default text color of list items is white, I want to change text color of text views in the list to black.

How should i do it?

like image 440
User7723337 Avatar asked Apr 06 '11 08:04

User7723337


People also ask

How do I change the color of my text in list view?

This example demonstrates how to change the color and font of Android ListView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change text color in ArrayAdapter?

Just create an anonymous subclass of ArrayAdapter and override getView(). Let super. getView() handle all the heavy lifting. Since simple_list_item_1 is just a text view you can customize it (e.g. set textColor) and then return it.


1 Answers

Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.

e.g. mytextview.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/tv"     android:textColor="@color/font_content"     android:padding="5sp"     android:layout_width="fill_parent"     android:background="@drawable/rectgrad"     android:singleLine="true"     android:gravity="center"     android:layout_height="fill_parent"/> 

and then use it with your ArrayAdapter as usual like

ListView lst = new ListView(context); String[] arr = {"Item 1","Item 2"}; ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,arr); lst.setAdapter(ad); 

This way you won't need to create a custom adapter for it.

like image 196
dharmin007 Avatar answered Sep 29 '22 01:09

dharmin007