0

I've searched around online for a solution to this problem and have tried out different ways of doing this but haven't come up with an answer. I have a list view and a custom row layout for the rows of the list. In my list row xml file there is a relative layout with an ImageView and text. Along with that I set the background of this relative layout to a certain shape and this works fabulous. Now at runtime I want to change this list row background to appear pressed inwards ( I was thinking just make the padding a little smaller or something). I have tried making another xml file for a pressed shape but it hasn't worked. I also tried to code this up by doing something such as this:

RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.relativerow);
                    rl.setBackgroundResource(R.drawable.item_shape_pressed);

I'm guessing I'd have to make my own onPressed method for this to work? Here is the shape xml file I am using for each list row background:

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_shape">
    <padding android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
    <corners android:radius="20dp" />
    <solid android:color="#FFF" />
</shape>

Here is the XML for each row in the list view. See where I sit the background attribute at the top.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/item_shape"
    android:padding="5dip" >


    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="60dip"
            android:layout_height="60dip"/>

    </LinearLayout>


    <TextView
        android:id="@+id/articletitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="16sp"
        android:textStyle="bold"/>


    <TextView
        android:id="@+id/articldesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/articletitle"
        android:textColor="#343434"
        android:textSize="13sp"
        android:textStyle="italic"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
   />


</RelativeLayout>

So, is there any way for me at runtime to change the background in this relative view from the normal item_shape to another pressed in item shape? If anyone could show me how to do this I would certainly appreciate it. This is just a small part of the app that has been bugging me recently but it would be a nice touch to make the app look more complete.

Tastybrownies
  • 897
  • 3
  • 23
  • 49
  • 1
    Use a `listSelector` properly setup. Questions related can be found [here](http://stackoverflow.com/q/2183447/1051783) or [here](http://stackoverflow.com/q/2562051/1051783) – gunar Feb 02 '14 at 20:16
  • Show us your ListAdapter – alecnash Feb 02 '14 at 21:19

1 Answers1

2

Try with create your own selector for the ListView, for example listview_item_selector.xml in res/drawable folder and add this selector in the background of your RelativeLayout.

The following code will be: listview_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_shape_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/item_shape_focused" android:state_focused="true" />
    <item android:drawable="@drawable/item_shape" />
</selector>

And in your RelativeLayout put:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/listview_item_selector"
    android:padding="5dip" >

    .....
iarroyo
  • 2,354
  • 24
  • 23