Card View background color affects shadow color
11,574
I ran into the exact same problem and solved it by removing the alpha portion of my hex code.
Example: #AA333333 removing the AA. Of course use the hex color that you need without the alpha.
Author by
Greg Peckory
Updated on June 26, 2022Comments
-
Greg Peckory about 2 years
I have my Card View set up like this:
android:layout_marginTop="2dp" android:layout_marginLeft="6dp" android:layout_marginRight="6dp" card_view:cardCornerRadius="2dp" card_view:cardElevation="6dp" card_view:cardUseCompatPadding="true" card_view:contentPadding="10p"
Without a background color my Card View looks perfect like this:
However, when I add the simple property:
card_view:cardBackgroundColor="#xxxxxxxx"
the shadows change significantly in terms of color, transparency, blur etc.
How might I go about fixing this? I tried using a
RelativeLayout
as the background and changing the color there, so it wouldn't affect the shadows... but that affected the rounded corners.Any ideas? Thanks for the help!
-
Greg Peckory over 8 yearshow might I achieve this programatically ?
-
Nick Isaacs over 8 yearsCardView card = findView... Card.setBackground()
-
Greg Peckory over 8 yearsThat is exactly what I tried, except the
RelativeLayout
will fit inside theCardView
but won't expand inside it. As a result I don't get my desired rounded corners. -
Ilya Gazman over 8 years@GregPeckory You can use a drawable shape to get rounded corners. Any way you should try more tower this direction, it should work.
-
Greg Peckory over 8 yearsI need to create a
drawable
for this. Do I just set the color there? -
Nick Isaacs over 8 yearsSorry, Please use card.setBackgroundColor(Color.rgb(255, 0, 0)); . Specify the RBG values inside the rgb() . setBackgroundColor takes int values
-
Greg Peckory over 8 yearsI have tried this, and it results in a large rectangle with no shadow or rounded corners.
-
Nick Isaacs over 8 yearsallright. Have you tried this : stackoverflow.com/questions/26561122/…
-
Greg Peckory over 8 yearsYes, this was initially what was causing the issue
-
Daxesh Vekariya about 5 yearsNot Exact solution.
-
Taras Lozovyi over 3 yearsThat's exactly what caused the problem. Elimination of alpha part of color solved the issue for me.
-
yeshu almost 3 yearsIs there a way to solve it when the color is partly translucent(with some alpha)??