0

Is there any onClick() event at a CardView?

I didn't find any solution on the web.

Example:

<androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            app:cardCornerRadius="8dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="30dp"
            android:layout_height="80dp"/>

So when I click on the cardview the layout_height goes up to 160dp. Is there any possibility?

Dharman
  • 30,962
  • 25
  • 85
  • 135
mcbabo
  • 13
  • 6
  • okay thanks i will try it – mcbabo Dec 11 '20 at 00:06
  • i deleted my comment since answers shouldn't be commented and you edited your question to make it more clear. if you want example code, it is below with my answer :) you didn't ask how to set the height of a cardview, so i only included code for what you asked for. – null_awe Dec 11 '20 at 00:11

1 Answers1

0

You can set onClick() either programmatically or in the xml file. Here are them both ways:

In the xml file: (Make sure you have a method called changeHeight with a parameter of View view defined)

<androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            app:cardCornerRadius="8dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="30dp"
            android:layout_height="80dp"
            android:onClick="changeHeight"
            android:id="cardview"/>

Notice that I set an id for the CardView here. You want to do this if you are going to set an onClickListener() programmatically.

Programmatically:

CardView cv = (CardView) findViewById(R.id.cardview); // using the id we set
// earlier.
cv.setOnClickListener(v -> {
    // set the height
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
null_awe
  • 483
  • 6
  • 17
  • okay thanks and how do i change the height in java? i thought it could be cardview.layout_height but it doesnt.. – mcbabo Dec 11 '20 at 00:21
  • To change the layout_height field, you can have a look at this question. https://stackoverflow.com/questions/10159372/android-view-layout-width-how-to-change-programmatically Basic idea is to have a new `LayoutParams` instance with the `height` value with your desired value and set it to your `CardView` object. – Hyun I Kim Dec 11 '20 at 00:26