I was trying to create a simple list view that has some padding for each row.
I did it in a really simple way, but I’m not sure if it is the right™ way to do it.
This is what I got (code is below). As you can see when you tap a row, it is fully highlighted, and I want (if possible) to highlight only the view inside it (the one with the gray background, that is a TextView)

this is the code for the Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dp" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_events" />
</LinearLayout>
and this is the code of the list item layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#ccc"
android:text="placeholder" />
</FrameLayout>
Thanks for any reply!
PS. I didn’t upload the Activity, it is a simple ListActivity.
UPDATE
Resolved (thanks to Jorge Aguilar). The new code is this:
List item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="@drawable/event_focused"
android:text="placeholder" />
</FrameLayout>
Event focused file (which is in the drawable directory):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@android:color/black" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@android:color/darker_gray" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@android:color/darker_gray" /> <!-- hovered -->
<item android:drawable="@android:color/darker_gray" /> <!-- default -->
</selector>
I also added this:
android:listSelector="@android:color/transparent"
to my ListView 🙂
Well you can add on the onitemselected method some code to find the textview by id or something similar and then change the background color. or is something different what you want ?
UPDATE:
If you need to change it from within the xml file, maybe this post can help you achieve it. just skip to the last post and hopefully you will find your answer there.