franck garros
franck garros

Reputation: 1

How to do to make clickable only parent but not children?

Here is my XML

<android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp">

        <LinearLayout
            android:id="@+id/commander_image_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical"
            android:background="?attr/selectableItemBackground"
            android:padding="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="15dp"
                android:paddingEnd="15dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingStart="15dp"
                android:text="@string/image_chooser_txt"
                android:textAlignment="center" />

            <ImageView
                android:id="@+id/commander_result_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:minHeight="150dp"
                android:minWidth="200dp"
                android:padding="5dp"
                android:scaleType="centerInside"
                android:src="@mipmap/default_image"
                android:contentDescription="@string/chosen_image" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

I would like to set android:background="?attr/selectableItemBackground" on the LinearLayout if i click on it or on the imageView.

Is there any solution ? It is like pointer-event: none in css

Upvotes: 0

Views: 794

Answers (2)

franck garros
franck garros

Reputation: 1

Edit

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">
    <LinearLayout
        android:id="@+id/commander_image_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:addStatesFromChildren="true"
        android:background="?attr/selectableItemBackground"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="15dp"
            android:paddingEnd="15dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingStart="15dp"
            android:text="@string/image_chooser_txt"
            android:textAlignment="center" />
        <ImageView
            android:id="@+id/commander_result_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:contentDescription="@string/chosen_image"
            android:minHeight="150dp"
            android:minWidth="200dp"
            android:padding="5dp"
            android:scaleType="centerInside"
            android:src="@mipmap/default_image" />
    </LinearLayout>
</android.support.v7.widget.CardView>

With android:addStatesFromChildren="true", selectableItemBackground works for children. Then I just have to set my imageView non clickable programmatically (doesn't work in xml) and it works like I wanted to

Upvotes: 0

Entreco
Entreco

Reputation: 12900

By default, a LinearLayout is not clickable, and will not receive touch events. You can simply indicate that your LinearLayout should be clickable, an d your ImageView is not by adding:

<LinearLayout
        android:id="@+id/commander_image_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        android:background="?attr/selectableItemBackground"
        android:padding="15dp"
        android:focusable="true"
        android:clickable="true">

          <ImageView
            android:focusable="false"
            android:clickable="false"
            />
       />

Upvotes: 1

Related Questions