Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color of simple ListView Multiple choice Android

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);
like image 581
user2210356 Avatar asked Mar 26 '13 06:03

user2210356


People also ask

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.

What is setAdapter in Android?

In order to display items in the list, call setAdapter(android. widget. ListAdapter) to associate an adapter with the list. For a simple example, see the discussion of filling an adapter view with text in the Layouts guide. To display a more custom view for each item in your dataset, implement a ListAdapter.


1 Answers

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"
                     />
like image 193
GrIsHu Avatar answered Nov 07 '22 02:11

GrIsHu