Question

So I am using the following so I can change the on click color of my listview item. However, I am annoyed at what is happening when I return to my activity after the click - the color stays the same as when I clicked on it for about a second and then it refreshes to the normal background. Also, if I hold the listview item and slide my finger away, the listview item stays as it is continuously clicked on and does not return until I tap on another listview item. How can I only change the background color of the listview item on tap only and not have it stay the same after I switch back to the same activity? Here is my code. Thanks!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_repeatable"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CategoriesActivity">

    <ListView
        android:id="@+id/categoriesList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@color/White"
        android:textAlignment="center"
        android:visibility="visible" 
        android:divider="@android:color/transparent"
        android:dividerHeight="10.0sp"
        android:drawSelectorOnTop="false"
        android:listSelector="@drawable/list_view_item_background_on_click">
    </ListView>
</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>

<!-- Layout for individual news entries in a list -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/list_view_item_background"
    android:gravity="center_vertical"
    android:paddingBottom="8.0dip"
    android:paddingLeft="12.0dip"
    android:paddingRight="3.0dip"
    android:paddingTop="8.0dip" >

    <ImageView
        android:id="@+id/imgUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:paddingLeft="2sp"
        android:src="@drawable/ic_menu_search_holo_light">
    </ImageView>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_toRightOf = "@+id/imgUrl"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="@color/White"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>



<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="10dp" />

    <stroke
        android:width="0.5dp"
        android:color="@color/Teal" />
    <solid android:color="@color/Teal" />
</shape>
Was it helpful?

Solution

Here seems to be a good tutorial But you want to use state drawables to change the color in different states such as focused, pressed, enabled, disabled

StateListDrawable Docs

Doing it this way, the color will change when you press on it but when you return the state won't be pressed so it will be the default color, which I believe is what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top