Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight ListView-Items

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);
}
like image 373
mad Avatar asked Dec 10 '10 19:12

mad


2 Answers

@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);

        }
like image 94
ununiform Avatar answered Oct 07 '22 23:10

ununiform


You should use a Selector.

This question and its answer might help....

like image 27
st0le Avatar answered Oct 07 '22 21:10

st0le