22

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.

Davideas
  • 3,226
  • 2
  • 33
  • 51

4 Answers4

12

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.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • Changing by overriding is more than enough, but you mean to override these values? - http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/frameworks/support/v7/cardview/res/values/colors.xml?av=f - In my color.xml I put my custom `#37000000` and `#03000000` - Is the system take them **automatically**? – Davideas Jul 10 '15 at 09:12
  • Yes it's these values, they will apply to all your CardViews. I believe you also need to set useCompatPadding to true to be able to see the color on newer Android versions as well. – BladeCoder Jul 10 '15 at 10:18
  • I tried now but it doesn't work. The resource comes from `android.support.v7.cardview.R.color.cardview_shadow_start_color` how it should take mine from my color.xml? Could you provide an example? – Davideas Jul 22 '15 at 21:29
  • I correct myself, it works only on pre-L, but useCompatPadding is set true. Could you provide an example for L? – Davideas Jul 22 '15 at 21:41
  • 1
    I'm sorry, I thought that setting "useCompatPadding" would make CardView use the pre-L implementation which uses the colors from the resources but it's not the case. On L CardView uses the native shadow and the native shadow color can't be customized. Your best option is probably to create a custom 9-patch image with built-in padding representing your custom colored shadow and use it as a background for a FrameLayout. – BladeCoder Jul 22 '15 at 22:59
2

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.

Lym Zoy
  • 951
  • 10
  • 16
1

I've used a small trick. One CardView is put behind another one. Both are the same, difference is card_view:cardElevation="10dp" for background one, and card_view:cardElevation="2dp" for faced one. The subtraction of elevation provides how long is your shadow, and color of the second CardView gonna be color of the shadow for first one.

Example:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_click_basement"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardBackgroundColor="@color/colorNewGreen"
    card_view:cardElevation="10dp"
    card_view:cardUseCompatPadding="true">

    <android.support.v7.widget.CardView
        android:id="@+id/view_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">
-3

Try this :-

android:outlineSpotShadowColor="@color/" android:outlineAmbientShadowColor="@color/"