0

I have a list where I have defined my own list view items with a custom layout. This layout has a background with a custom drawable.

My custom layout for the ListView item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/item"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" >
    ...
</RelativeLayout>

My custom drawable item.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- background: shadow -->
    <item>
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@color/itemShadowColor" />
        </shape>
    </item>

    <!-- foreground: surface -->
    <item android:bottom="2dp">
        <shape
            android:dither="true"
            android:shape="rectangle" >
            <corners android:radius="2dp" />

            <solid android:color="@color/itemBackgroundColor" />
        </shape>
    </item>

</layer-list>

Now this item is not clickable anymore.

Can you explain me why, and what I have to do to have the same behavior (selector with the blue background) like a button click?

Terry
  • 12,591
  • 12
  • 51
  • 84

1 Answers1

2

Define a selector with its pressed and default states in res/drawable folder(one of the state will be your @drawable/item). Set it as the bg of your list row layout.

See similar question and answer :Selector on background color of TextView

Edit: Best way to understand and apply something like google did is, to look into SDK and do similar things to that. For instance look at the btn_default_holo_dark drawable. It is a selector with states and yes it is a xml.

This is a selector taken from sdk (sdk\platforms\android-18\data\res\drawable\btn_default_holo_dark.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal_holo_dark" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_disabled_holo_dark" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed_holo_dark" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_focused_holo_dark" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal_holo_dark" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_disabled_focused_holo_dark" />
    <item
         android:drawable="@drawable/btn_default_disabled_holo_dark" />
</selector>

These are the images taken from sdk (sdk\platforms\android-18\data\res\drawable-xhdpi):
enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

When you apply this drawable/selector (@drawable/btn_default_holo_dark) to any view, you are going to have its states. I hope this sample makes my answer more clear.

Community
  • 1
  • 1
Devrim
  • 14,694
  • 4
  • 59
  • 70
  • But what drawable do I link so that the pressed and selected states look like the pressed button (with the blue overlay)? I appreciate code snippets... – Terry Nov 07 '13 at 21:07
  • I've edited my answer. You can use google's images at sdk. Or you can create them by a image editor. – Devrim Nov 07 '13 at 21:56
  • I understand it now. Still I find it quite annoying that we have to copy what's in the SDK instead of just being able to use it and adjust it a bit... Anyway, thanks a lot! – Terry Nov 07 '13 at 22:00
  • That was just a sample to make things clear. You can directly use android's resources with android.R.drawable. tags but you must consider that they are not going to be exist in all api levels and they can be changed for api levels. If you want same appearance for all targeted APIs you can then copy them to your res folder and use. Or you can create your own images with a graphical editor like photoshop, gimp. – Devrim Nov 08 '13 at 05:50
  • Are you sure? Because I cannot reference `@android:drawable/btn_default_normal_holo_dark` or any of these, and I have to write my own selector anyway, instead of just using a default one for the pressed state overlay. – Terry Nov 08 '13 at 08:50
  • "You can directly use android's resources with android.R.drawable. tags but you must consider that they are not going to be exist in all api levels and they can be changed for api levels." Try to set your project build target to API Level 18 (or over 11+). But if you are going to use it from android resources please consider my consideration;) – Devrim Nov 08 '13 at 09:35
  • Even with the `android.R.drawable` tag in the Java code it didn't find it. I'm API level 13, so the Holo items should be there. But they are not public, so I cannot access them like this. Anyway, your answer really helped and I could make it work with that. :) – Terry Nov 08 '13 at 14:09