user735855
user735855

Reputation: 173

How to Change List View Text color

In my application I want to change the list view text color. In this case I am using XML file for list view. Is it possible? If yes then give the example.

gender.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
 android:background="@drawable/topelg"
 >

      <ListView
        android:id="@+id/android:list"
        android:layout_marginTop="60dip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#000000"

        />

</LinearLayout>

Egender.java

package com.Elgifto;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

public class Egender extends ListActivity{
    Button b1;
    ListView lv;

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gender);

       // b1=(Button) findViewById(R.id.butt1);
        b1=new Button(this);
        b1.setText("Done");

       //b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        lv=(ListView) findViewById(android.R.id.list);

        lv.addHeaderView(b1);  

        b1.setOnClickListener(new OnClickListener(){
              public void onClick(View v)
              {
                  // To send a result, simply call setResult() before your
                  // activity is finished.

                  finish();
              }
        });

        lv.setAdapter(new ArrayAdapter&lt;String&gt;(this,
                android.R.layout.simple_list_item_single_choice, GENDER));

       lv.setBackgroundColor(Color.BLACK);
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setTextFilterEnabled(true);

    }

    private static final String[] GENDER = new String[] {
       "Male","Female"
    };
}

Upvotes: 5

Views: 14276

Answers (1)

wilkas
wilkas

Reputation: 1213

If you would like to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_single_choice to ArrayAdapter you should pass custom list item XML, which has different TextColor attribute.

For example, created custom_list_item.xml under folder Layout:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>

Then passed it to adapter:

new ArrayAdapter<String>(this, R.layout.custom_list_item, stringList)

I've got list, were all items are red.

Upvotes: 19

Related Questions