Android CardView padding and minHeight
Asked Answered
O

2

2

Today I updated support dependencies in my project and I found some issues with the new CardView.

Previous version:

compile 'com.android.support:cardview-v7:21.0.0-rc1@aar'

Current version:

compile 'com.android.support:cardview-v7:21.0.0@aar'

First of all it looks like minHeight doesn't work any more. One of the solutions is to put any ViewGroup inside the card and set the minimum height, but it looks like a bug for me.

The second issue for me are paddings. With the new library release paddings are larger. If you put some cards next to each other the gap between them is way to large. I've tried setting the padding to 0dp but it looks like the padding comes from 9.png file. The code pasted below doesn't change anything.

<android.support.v7.widget.CardView
    android:padding="0dp"
    ...

I've also tried with negative values but also nothing. Is there any way to solve this two issues for pre-Lollipop devices?

Ozalid answered 18/10, 2014 at 13:0 Comment(0)
S
7

About the padding stuff you have new information here: http://developer.android.com/reference/android/support/v7/widget/CardView.html

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

Since padding is used to offset content for shadows, you cannot set padding on CardView. Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the Card and children of CardView.

Sutherland answered 18/10, 2014 at 13:48 Comment(2)
You're right, the padding is calculated based on the elevation and corner radius. Looks like I've to find 3rd party card library because I cannot achieve suitable shadow without large padding around the card.Ozalid
or just use card_view:cardUseCompatPadding="true"Luge
M
0

I had a similar problem and I managed to resolve that way:

<androidx.cardview.widget.CardView 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:app="http://schemas.android.com/apk/res-auto" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:layout_marginHorizontal="@dimen/space_small" 
   app:contentPadding="@dimen/space_small" 
   app:cardCornerRadius="@dimen/space_mirror" 
   app:cardElevation="2dp" 
   app:cardUseCompatPadding="true" >

   <androidx.constraintlayout.widget.ConstraintLayout 
        android:id="@+id/items_wrapper" 
        android:minHeight="80dp" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent">
         
     .....

   </androidx.constraintlayout.widget.ConstraintLayout> 
</androidx.cardview.widget.CardView>
Macnair answered 9/8, 2021 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.