Brian D
Brian D

Reputation: 10143

How to indicate that a listview item is in focus?

I implemented a custom list view, but now there's no visual feedback that an element has been selected. I think this is trivial but I can't find anything on it -- can anyone provide tips on how to apply a default UI element-in-focus behavior to my listview items?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true">


    <TextView android:id="@+id/list_item"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:textSize="20sp"
        android:textColor="#FFFFFFFF"
        android:layout_height="wrap_content" 
        android:padding="20dp"/>


</LinearLayout>

From what responses I've gotten so far, I think I should clarify what I'm trying to do: I want to mimic the behavior that a listview item does (in the standard UI) when long-pressed. When you fling through it, it doesn't really do anything, but upon touching the item and holding it for any time at all, it turns colors [orange, in my case] and then quickly transitions to white.

And, upon further inspection, I think this might happen only when adding a Context Menu -- I'll try it out and edit this with the results.

Upvotes: 0

Views: 762

Answers (3)

Brian D
Brian D

Reputation: 10143

The way to do this is by adding a context menu. That will bring about my desired effect.

Upvotes: 1

Abhinav
Abhinav

Reputation: 39914

You can try setting a selector drawable for your listview row background.

android:background="@drawable/listview_bg"

The drawable needs to be something like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/list_bg_blue"/>
<item android:state_pressed="true" android:state_enabled="false" android:drawable="@drawable/list_bg_blue"/>
<item android:state_pressed="true" android:state_enabled="true" android:drawable="@drawable/list_bg_blue"/>    
<item android:state_enabled="false" android:drawable="@drawable/list_bg_blue"/>    
<item android:drawable="@drawable/list_bg_grey"/>

Also, try out RelativeLayout instead of LinearLayout.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234817

You might want to take a look at the article on Touch Mode.

Upvotes: 1

Related Questions