Change the text color of a simple ListView Multiple choice Android

I am developing a cricket application. My requirement is this: if I choose team 1, the list of the displayed country name should be displayed, and if I choose the name of the country in India, the list of players from India should be displayed, and in this I selected several players from this. I did everything. But my problem is that I use android.R.layout.simple_list_item_multiple_choice to select players. I use a simple list view, and the background of this list is a black image. And my list is like this

    <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="8.5"
    android:cacheColorHint="#00000000" 

     />

Now the problem is that the listview value shows black. I already have a black background image. And the value is also black. So it doesn’t look good. How to change the color of list values ​​to white without changing the user adapter.

And this is my adapter class

 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,playersName);
    lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lvview.setAdapter(adapter);
+5
source share
4 answers

You need to create a Custome TextViewto change the color of all ListView items, instead of passing the default android.R.layout.simple_list_item_multiple_choiceto ArrayAdapter, you must pass a custom XML list item that has a different TextColor attribute.

For example, the created custom_list_item.xml in the Layout folder:

   <?xml version="1.0" encoding="utf-8"?>
   <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/textView"
       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="#FF00FF"
       />

Then it passed it to the adapter, as shown below:

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

Edition:

Here is the code that works fine, I tested.

   lv.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName));
    lv.setBackgroundColor(Color.BLACK);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3)
        {
             my_sel_items = new String("Selected Items");
                SparseBooleanArray a = lv.getCheckedItemPositions();
                for (int i = 0; i < a.size(); i++) {
                    if (a.valueAt(i)) {
                        my_sel_items = my_sel_items + ","
                                + (String) lv.getAdapter().getItem(i);
                    }
                }
                Log.v("values", my_sel_items);
        }
    });

View List Layout

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

styles.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    ...other styles

//

    <style name="ListFont" parent="@android:style/Widget.ListView">
    <item name="android:textColor">#FFFFFF</item>
    </style> 

     ...other styles

    </resources>

xml style="@style/ListFont" listview

0

check below code:

package com.example.mapsdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class MainActivity extends Activity {

    ArrayList<String> a = new ArrayList<String>();

    private ListView lView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fillarray();
        lView = (ListView) findViewById(R.id.listView1);
        lView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, a));
        lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lView.setOnItemClickListener(new OnItemClickListener() {

            private String my_sel_items;

            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                // List list = new ArrayList();
                my_sel_items = new String("Selected Items");
                SparseBooleanArray a = lView.getCheckedItemPositions();

                for (int i = 0; i < a.size(); i++) {
                    if (a.valueAt(i)) {
                        my_sel_items = my_sel_items + ","
                                + (String) lView.getAdapter().getItem(i);
                    }
                }
                Log.v("values", my_sel_items);
            }
        });
    }

    private void fillarray() {
        // TODO Auto-generated method stub
        a.clear();
        a.add("a");
        a.add("b");
        a.add("c");
        a.add("d");
        a.add("e");

    }

}

your result in the magazine

03-26 12:25:06.106: V/values(3301): Selected Items,a
03-26 12:25:06.810: V/values(3301): Selected Items,a,b
03-26 12:25:07.466: V/values(3301): Selected Items,a,b,c
03-26 12:25:08.136: V/values(3301): Selected Items,a,b,c,d

Edited by:

check this link you can use any font color and background color of the list in this code.

0
source

Luksprog's solution is acceptable and not complicated. But without a line

if (position == 1) 
0
source

All Articles