Skip to content Skip to sidebar Skip to footer

How To Change Listview Selected Items Color From Default To Red Without Using Selector

I am developing an App for cricket. In that my aim is to select players for the particular team in ListView. Here I can able to select multiple players from the list. I am using si

Solution 1:

Got Answer by using the below code

In mainActivity adapter class

   adapter=newArrayAdapter<String>(getApplicationContext(), R.layout.text_view,R.id.textView1,players);

    lvview.setAdapter(adapter);

main.xml look like below

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

And my custom layout field is like below

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:text="TextView"
    android:textColor="#ffffff" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:focusable="false"
  android:focusableInTouchMode="false"
    />

And in MainActivity onitem click listener for ListView I called the custom layout view and the code is given below

            lvview.setOnItemClickListener(newOnItemClickListener() {

        @OverridepublicvoidonItemClick(AdapterView<?> l, View v, int position,
                long id) {
            // TODO Auto-generated method stubSparseBooleanArraychecked= lvview.getCheckedItemPositions();  
            checkedText=(CheckBox) v.findViewById(R.id.checkBox1);
            checkedList=(TextView) v.findViewById(R.id.textView1);
            if(checkedText.isChecked()==false)
            {
                counter_selected++;
                checkedText.setChecked(true);
                checkedList.setTextColor(Color.RED);
                selectedCounterText.setText("" + counter_selected);                 

            }
            else
            {
                counter_selected--;
                checkedText.setChecked(false);
                 checkedList.setTextColor(Color.WHITE);
                 selectedCounterText.setText("" + counter_selected);        
            }
        }
    });

And it solved my problem..

Solution 2:

you have to use statelist drawable to define background on events, and for easiness Here is the very similar question, that might help you. listview

Solution 3:

The problem is it is not possible to do it via simple adapter (which you have used).

You need to define custom row items. You need to define custom XML layouts for each list item which gets bound to the listview. Thereby, you need to make a customAdapter which has a getView() method. In it you can easily fetch the child TextView of the row and change its Properties.

Follow this tutorial.

Post a Comment for "How To Change Listview Selected Items Color From Default To Red Without Using Selector"