An SO User
An SO User

Reputation: 25018

My ListView lost its behavior after I changed background color?

All I have done is change the color of the background and I can no longer get the effect when I click the list items. What do I need to change ? The XML is below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:scaleType="centerCrop" 
        android:layout_margin="10dp" 
        android:src="@drawable/file" />

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/image"
        android:layout_marginTop="30dp"
        android:layout_toRightOf="@+id/image"
        android:background="@null"
        android:includeFontPadding="true"
        android:textColor="#000000" />


</RelativeLayout>  

All I want to do is to have the look as below but with the change in color when I click the list item:
enter image description here

Update:

<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: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=".MainActivity" >

    <ListView android:id="@+id/list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#FFFAF0"
        android:dividerHeight="1dp"
        android:layout_margin="0dp"
        android:divider="#808080"/>

</RelativeLayout>

Upvotes: 0

Views: 348

Answers (1)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Well, what I'do is:
instead of using a fixed color as the background for your custom item, like in

android:background="#FFFFFF">

I'd use a stelist drawable, as in:

android:background="@drawable/item_states">

Of, course, you'll need a new xml in your drawable folder, called whatever (I called it item_states, in the example)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
        android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

Upvotes: 1

Related Questions