Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    };
}
like image 673
user735855 Avatar asked May 23 '11 10:05

user735855


People also ask

How do you change the color of text in list view?

xml version = "1.0" encoding = "utf-8" ?> Below is the code for the Item layout for displaying the item in the ListView. We have added textColor and textSize attributes to the TextView to change the text color and size.

What is property changes the color of text android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

What is simple_list_item_1?

R. layout. simple_list_item_1 , which is a layout built into Android that provides standard appearance for text in a list, and an ArrayList called restaurants (not seen here).

What is the use of adapter object in Android SDK explain Arrayadapter in detail?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


1 Answers

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.

like image 185
wilkas Avatar answered Oct 14 '22 07:10

wilkas