How to highlight ListView-Items

mad picture mad · Dec 10, 2010 · Viewed 30.5k times · Source

i got the following problem.

i have a ListView with custom rows consisting of an imageview and a textview. the textview's xml code is

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColorHighlight="#FEC403"
/>

then i have an itemclicklistener that works fine and i want to highlight the textview that has been clicked by doing the following.

public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            String pathtofile = (String) adaptview.getItemAtPosition(position);
            View rowview = (View) adaptview.getChildAt(position);
            rowview.setSelected(true);}

i wanted the highlight color to be "#FEC403" in the xml (that is a light orange) but the highlightcolor still is gray. so how to set the highlightcolor correctly?

thanks in advance

EDIT:

here is how i did it finally:

this is my ListView Item xml file:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rowselector"
>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/musicicon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/musicicon"
    android:paddingLeft="3px"
/>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="26px"
    android:layout_marginLeft="3px"
    android:singleLine="true"
    android:ellipsize="end"
    android:focusable="false"
/>

and the rowselector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
    android:drawable="@color/orange" />
</selector>

and at last my OnItemClick is very short now:

@Override
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position,
                long id) {
            //TODO: ACTIONS
            clickedview.setSelected(true);
}

Answer

ununiform picture ununiform · Jun 25, 2012
@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
        {
            for(int a = 0; a < parent.getChildCount(); a++)
            {
                parent.getChildAt(a).setBackgroundColor(Color.BLACK);
            }

            view.setBackgroundColor(Color.RED);

        }