android linearlayout background selector
Asked Answered
U

1

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...???

Unarmed answered 26/9, 2011 at 8:15 Comment(6)
does it work if you put it outside the scrollview?Succursal
I guess some controls in scrollview consumes pressed event so your child LinearLayout cannot get touch.Japhetic
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...Unarmed
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"Unarmed
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...Rudimentary
Exact duplicate of #6866230Brownie
R
32

You must make the LinearLayout clickable.

android:clickable="true"
Reportage answered 10/7, 2012 at 13:30 Comment(1)
When I click one layout, all layouts show state pressed. This solve that issue too. Thanks!Excruciating

© 2022 - 2024 — McMap. All rights reserved.