13

I am adding some Linearlayout views in a scrollview by inflating them dynamically. I have set the background of the added LinearLayout to a selector list. But after adding to scrollview, when I press the selected view, it does not show any affect of selector list. The example XMLs I am using are:

Selector file: selector_file

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/listbg"
      android:state_pressed="true" />
<item android:drawable="@drawable/bgsmall"/>
</selector>

And I am inflating the following view and adding to l1 Linearlayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_file"
android:gravity="center_vertical"
>
<ImageView android:id="@+id/image1" android:layout_width="100dip" android:layout_height="75dip"/>
<TextView android:id="@+id/textitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000"></TextView>
</LinearLayout>

And the ScrollView to which the above inflated views are being added is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/l1"  android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">

</LinearLayout>
</ScrollView>
</LinearLayout>

Any Idea...???

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • does it work if you put it outside the scrollview? – blessanm86 Sep 26 '11 at 08:21
  • I guess some controls in scrollview consumes pressed event so your child LinearLayout cannot get touch. – anticafe Sep 26 '11 at 09:04
  • 1
    Actually I am setting the selector to the background of a Linearlayout. If I use the same selector for a button, it works but not for LinearLayout... – Khawar Raza Sep 26 '11 at 10:23
  • 1
    Thanks to all. I have got the solution. I have added the following lines in the Linearlayout of the inflated file: android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" – Khawar Raza Sep 26 '11 at 10:28
  • 1
    android:clickable="true" fixes the problem of the button not looking pressed when it's pressed. But if you're using setSelected(true) then state_selected drawable doesn't work anymore... – User Jun 06 '12 at 07:37
  • Exact duplicate of http://stackoverflow.com/questions/6865730/selector-color-on-linearlayout – Paul Lammertsma Aug 24 '12 at 13:31

1 Answers1

32

You must make the LinearLayout clickable.

android:clickable="true"
Alex K
  • 22,315
  • 19
  • 108
  • 236
christiandeange
  • 1,175
  • 12
  • 17