JayVDiyk JayVDiyk - 9 months ago 91
Android Question

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?

Answer Source

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);