5
votes

I am developing an app for cricket. My requirement is like this, if i select team 1 the list of available country name has to display and if i select a country name as India the list of player from India has to displayed and in that i have select a multiple players from that. I have done everything. But my problem is i am using android.R.layout.simple_list_item_multiple_choice for selecting players. I am using simple list view and the background of that list is black image. And my listview is like that

    <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 the listview value is showing black in color. Already i have black background image. And the value also black in color. So its not looking good. How to change the color of listview values to white without changing o custom 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);
4

4 Answers

8
votes

You have to create Custome TextView to change color of all ListView items, instead of passing default android.R.layout.simple_list_item_multiple_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/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 passed it to adapter like as below:

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

EDITED:

Here is the code which is working fine i have 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);
        }
    });

Layout of listview

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

styles.xml

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

//add this

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

     ...other styles

    </resources>

at layout xml put this attribute style="@style/ListFont" to listview

0
votes

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 Log

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:

check this link you can use whatever font colour & listview background colour in this code.

0
votes

Luksprog solution is acceptable and not difficult. But without the line

if (position == 1)