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.
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.
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? 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With