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.

Share:
11,574
Greg Peckory
Author by

Greg Peckory

Updated on June 26, 2022

Comments

  • Greg Peckory
    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:

    enter image description here

    However, when I add the simple property:

    card_view:cardBackgroundColor="#xxxxxxxx"
    

    the shadows change significantly in terms of color, transparency, blur etc.

    enter image description here

    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
    Greg Peckory over 8 years
    how might I achieve this programatically ?
  • Nick Isaacs
    Nick Isaacs over 8 years
    CardView card = findView... Card.setBackground()
  • Greg Peckory
    Greg Peckory over 8 years
    That is exactly what I tried, except the RelativeLayout will fit inside the CardView but won't expand inside it. As a result I don't get my desired rounded corners.
  • Ilya Gazman
    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
    Greg Peckory over 8 years
    I need to create a drawable for this. Do I just set the color there?
  • Nick Isaacs
    Nick Isaacs over 8 years
    Sorry, Please use card.setBackgroundColor(Color.rgb(255, 0, 0)); . Specify the RBG values inside the rgb() . setBackgroundColor takes int values
  • Greg Peckory
    Greg Peckory over 8 years
    I have tried this, and it results in a large rectangle with no shadow or rounded corners.
  • Nick Isaacs
    Nick Isaacs over 8 years
    allright. Have you tried this : stackoverflow.com/questions/26561122/…
  • Greg Peckory
    Greg Peckory over 8 years
    Yes, this was initially what was causing the issue
  • Daxesh Vekariya
    Daxesh Vekariya about 5 years
    Not Exact solution.
  • Taras Lozovyi
    Taras Lozovyi over 3 years
    That's exactly what caused the problem. Elimination of alpha part of color solved the issue for me.
  • yeshu
    yeshu almost 3 years
    Is there a way to solve it when the color is partly translucent(with some alpha)??