Question

I have an app with ActionBarSherlock with custom view, the view have some clickable linear layout, but they're not responding visually to tapping/clicking. The custom view is as follows:

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:duplicateParentState="true"
        android:layout_gravity="right"
        android:background="@drawable/toolbar_button_background_drawable"
        android:padding="10dp"
        android:gravity="center" >

        <TextView
            android:duplicateParentState="true"
            android:id="@+id/decrease_fontsize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="4dp"
            android:clickable="true"            
            android:text="-A"
            android:textColor="#333"
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>

with the following drawable: (toolbar_button_background_drawable.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/hover" android:state_selected="true" />
    <item android:drawable="@drawable/hover" android:state_pressed="true" />
</selector>

hover.9.png is just a background...

but when I tap on this text, the onclick listener is triggered but visually the LinearLayout is not changed

Was it helpful?

Solution

When you set android:duplicateParentState="true" to a view, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself. So the parent Layout should be clickable.

Just add android:clickable="true" to the parent layout of the inner LinearLayout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top