Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView cardUseCompatPadding

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?

like image 661
JayVDiyk Avatar asked Jan 07 '16 13:01

JayVDiyk


People also ask

What is CardView used for?

CardView is a new widget in Android that can be used to display any sort of data by providing a rounded corner layout along with a specific elevation. CardView is the view that can display views on top of each other. The main usage of CardView is that it helps to give a rich feel and look to the UI design.

What is cardUseCompatPadding?

For a more consistent UI, use cardUseCompatPadding=true. CardView adds additional padding to draw shadows on platforms before Lollipop. This may cause Cards to have different sizes between Lollipop and before Lollipop.

What is CardView elevation?

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.


1 Answers

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);
like image 60
Jahnold Avatar answered Sep 30 '22 19:09

Jahnold