user4414636
user4414636

Reputation:

How to make only the child of a cardview clickable, and not the whole cardview itself?

Here's the layout of the cardview in context:

<android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="-25dp"
        android:clickable="true"
        app:cardUseCompatPadding="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardBackgroundColor="@color/md_white_1000"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/icon"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxLines="4"
                android:textColor="@color/md_teal_900"
                android:textSize="15sp"
                android:textStyle="bold" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:clickable="true"
                android:id="@+id/icon"
                android:src="@mipmap/ic_download" />


        </RelativeLayout>
    </android.support.v7.widget.CardView>

I want only the icon (imageview) to be clickable. This cardview is part of a recyclerview, and I can only make the whole card responsive to click, whereas I want only the imageview to be clickable.

Upvotes: 1

Views: 888

Answers (2)

Michele Lacorte
Michele Lacorte

Reputation: 5363

Set OnClickListener to ImageView, here an example:

ImageView image = (ImageView) findViewById(R.id.icon);
image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do operation
        }
});

EDIT:

If you using onClick() method:

@Override
public void onClick(View v) {
    switch(v.getId()){

        //Add this case to your switch
        case R.id.icon:

           //Do operation

        break;
    }
}

And as @vspallas says do not forget to add Listener

image.setOnClickListener(this);

Upvotes: 0

Vasileios Pallas
Vasileios Pallas

Reputation: 4867

You can do @Michele Lacorte's answer with onClick() method and then

ImageView image = (ImageView) findViewById(R.id.icon);
image.setOnClickListener(this);

Upvotes: 1

Related Questions