Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android CardView with a custom shadow color

Is it possible to change the color of the shadow around the CardView? Mainly used to mark selected the card as it were lighted on?

Should be valid on L and pre-L devices.

like image 581
Davideas Avatar asked Jul 08 '15 14:07

Davideas


People also ask

How do I change the color of my shadow in CardView?

Well, it is not possible to change the color of the shadow of cardview before API 28 but we can add a custom shadow behind a layout. You need to use a drawable background ( shadow. xml ) in the parent layout which is looking like a shadow. You can replace FF46A9 in shadow.

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

What is cardUseCompatPadding?

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 card elevation?

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.


2 Answers

CardView shadow colors are defined in the resources of the CardView library. You can override them by redefining the resource value in your own project but you can not change them dynamically by code.

Edit: overriding the resource value only affects pre-Lollipop devices. On Lollipop and above, CardView always uses the native shadow implementation whose color cannot be changed.

like image 92
BladeCoder Avatar answered Sep 19 '22 06:09

BladeCoder


Update: Check my modification.


Here's a workaround:

Copy the source code of CardView. Then create your own Android Library Module and use this module instead of support library. After these, comment or remove code in CardView like below:

static {
//        if (Build.VERSION.SDK_INT >= 21) {
//            IMPL = new CardViewApi21Impl();
//        } else
            if (Build.VERSION.SDK_INT >= 17) {
            IMPL = new CardViewApi17Impl();
        } else {
            IMPL = new CardViewBaseImpl();
        }
        IMPL.initStatic();
    }

That is, you will use compat-version CardViewApi17Impl even when api is 21 or higher. Then, you can define your own cardview_shadow_start_color and cardview_shadow_end_color to override those in class RoundRectDrawableWithShadow. Furthermore, you can make that more customizable.

Hope can help someone.

like image 44
Lym Zoy Avatar answered Sep 23 '22 06:09

Lym Zoy