Reputation: 1291
Please see the layout below. I want to show a selection highlight on the entire LinearLayout
if the user touches the ImageView
or preferably entire layout. This is like a onclick effect. Although I have a selector its not working. How can I make this work?
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="@drawable/layout_selector"
android:orientation="vertical">
<ImageView
android:id="@+id/btnFromEasy"
android:layout_width="@dimen/normal_button_size"
android:layout_height="@dimen/normal_button_size"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/animals" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/easy"
android:textColor="@color/subtitle_text_color"
android:textSize="@dimen/text_size_small"
android:textStyle="bold" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="Blue" />
</item>
<item>
<color android:color="White" />
</item>
</selector>
Upvotes: 1
Views: 840
Reputation: 54
Add android:clickable="true" to properties of LinearLayout and it work
Your code:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="@drawable/layout_selector"
android:orientation="vertical">
-> add
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="@drawable/layout_selector"
android:orientation="vertical"
android:clickable="true">
Line android:layout_width="0dp"
makes the layout can't be visible so I change it to "wrap_content"
Upvotes: 1
Reputation: 60923
Try this simple code.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/background_image_pressed" /> // image
<item
android:drawable="@drawable/background_image_default" /> // image
</selector>
and inside LinearLayout add android:clickable="true"
<LinearLayout
...
android:clickable="true"
...>
Hope this help.
Upvotes: 2