Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight ListView item on touch?

I have a simple ListView and I want each of it items to be highlighted on user's touch. I thought this should happen by default but it isn't. Can you advice?

ListView xml:

<ListView
    android:id="@+id/list_view"     
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:divider="#206600"
    android:dividerHeight="2dp"
    android:smoothScrollbar="true"
    android:background="#ffffff"
    >
</ListView>

And code of my Adapter:

private class MyAdapter extends ArrayAdapter<Task> {
        private LayoutInflater mInflater;

        public MyAdapter(Context context, int resource, List<Task> list) {
            super(context, resource, list);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                v = mInflater.inflate(R.layout.list_item, null);
            }

            Task task = taskList.get(position);

            /* Setup views from your layout using data in Object here */

            return v;
        }
like image 269
Eugene Avatar asked Mar 09 '11 15:03

Eugene


People also ask

How do I highlight a row in Android?

Tap the gray box at the top-left corner of the spreadsheet. It's above the first row and to the left of the first column. This highlights the entire spreadsheet.


2 Answers

You may want to post your actual row layout code, but I suspect the problem will be that you set a background color on your list row. By default, the selectors are drawn behind the list items (which looks nicer, since the highlight color is behind the text). Either don't set a background color on your list items, or set it to draw the selector on top instead with ListView's drawSelectorOnTop XML attribute.

EDIT: If you really must have an opaque background for the default state and don't want to use drawSelectorOnTop, you can also try this: Set a background on your list rows, but use a StateListDrawable to use @android:drawable/list_selector_background for all but the default state (you can define an xml file in your drawables folder for this; see the StateList documentation).

You could also nest a layout inside your outer backgrounded row layout with its background set to @android:drawable/list_selector_background; that way the background would draw on top of your background, but below the content.

like image 91
Yoni Samlan Avatar answered Sep 25 '22 11:09

Yoni Samlan


ListViews do not retain a visual indication of focus (or selection) while in touch mode. You will only see this when you use the hardware keyboard or controls to navigate your UI.

See the Google Touch Mode Android Blog article for more details.

So, if you are only using touch mode, you will never see focus or selection on ListViews.

like image 27
RivieraKid Avatar answered Sep 21 '22 11:09

RivieraKid