CardView cardUseCompatPadding
Asked Answered
G

2

9

I am developing an Android app for both Lollipop and Previous versions.

I am using CardView (This cardView does not have any child, it simply placed behind my View) to create shadow.

But the problem arise when it runs on the pre Lollipop devices.

so I set cardUseCompatPadding to true. I am wondering if I could get the value of this compat padding?

Is there anywhere I could find the reference to the value?

Gupton answered 7/1, 2016 at 13:23 Comment(0)
G
16

The compat padding added to the CardView depends on the elevation and the radius of the corners you have set. You can find the actual calculation in the RoundRectDrawableWithShadow class in the support library.

You can calculate it at runtime using something like:

    float elevation = cardView.getMaxCardElevation();
    float radius = cardView.getRadius();
    double cos45 = Math.cos(Math.toRadians(45));

    int horizontalPadding = (int) (elevation + (1 - cos45) * radius);
    int verticalPadding = (int) (elevation * 1.5 + (1 - cos45) * radius);
Glauce answered 7/1, 2016 at 14:12 Comment(1)
I don't think this necessarily holds true when elevation is 0 and radius is 0. There is still some non-zero padding being set when both of these attributes are set to 0dp, so this formula doesn't hold up in that case. Suggestions?Haulm
F
0

Updates the backward compatible maximum elevation of the CardView.

Calling this method has no effect if device OS version is Lollipop or newer and getUseCompatPadding() is false. Use this code

android:elevation="3dp"or app:elevation="3dp" to your cardview

CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms.

Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners. Instead, it adds padding to avoid such intersection (See setPreventCornerOverlap(boolean) to change this behavior).

Before Lollipop, 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 CardView and children of CardView.

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before Lollipop and after Lollipop. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms Lollipop and after as well, you can call setUseCompatPadding(boolean) and pass true.

To change CardView's elevation in a backward compatible way, use setCardElevation(float). CardView will use elevation API on Lollipop and before Lollipop, it will change the shadow size. To avoid moving the View while shadow size is changing, shadow size is clamped by getMaxCardElevation(). If you want to change elevation dynamically, you should call setMaxCardElevation(float) when CardView is initialized.

Foppish answered 14/11, 2016 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.