Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing CardView shadow color

This question was asked on SO many times, but still I didn't find a good solution for this problem.

Why do I need this to do? Well because project me and my team develops has iOS style.

What did I try?

  1. 9.pathch shadow generator but 9.pathes are essentially pngs and it gives me no flexibility and if I'll use this approach I should edit margins everywhere.
  2. Carbon library it supports custom shadows and they get drawn outside of view borders, but there is issue regarding rounded rectangles, when library doesn't draw shadow for rounded corners.
  3. using old CardView implementation and overriding its shadow color, but it gets drawn inside of card bounds, so it isn't option.

So is there a way to change shadow color of CardView with minimum edits of all layout files and with drawing shadow outside of the view like original CardView does?

like image 345
hotHead Avatar asked Jun 23 '18 14:06

hotHead


People also ask

How do I change the color of my card shadow?

Use Fake Shadow. xml ) in the parent layout which is looking like a shadow. You can replace FF46A9 in shadow. xml to change the color of shadow. Also android:backgroundTint="@color/colorShadow" works but you have to adjust colors alpha 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 outlineSpotShadowColor?

outlineSpotShadowColor.xmlSets the color of the spot shadow that is drawn when the view has a positive Z or elevation value. By default the shadow color is black.

What is CardView?

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.


1 Answers

Consider this thread in twitter, where Nick Butcher talks about how to implement the feature:

enter image description here

See outlineAmbientShadowColor, outlineSpotShadowColor, spotShadowAlpha and ambientShadowAlpha attributes for details. Unfortunately, that's possible from API 28 onwards.

For lower APIs Nick has shared a gist. Here's the result:

Running on API 21

This technique isn't directly connected to CardView, it can be applied to any View.

like image 138
azizbekian Avatar answered Sep 25 '22 17:09

azizbekian