5

I try to implement the Material Design's elevation concept to my RecyclerView.

For each item of the RecyclerView I use this

<?xml version="1.0" encoding="utf-8"?>
<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/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="0dp"
    android:elevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        <TextView
        .../>

        <CheckBox
        .../>
    </RelativeLayout>
</android.support.v7.widget.CardView>

When I select item, I apply programmatically a drawable to the background for effect, and I set the elevation to 8dp as advised by Material Design specifications.

        val draw = ResourcesCompat.getDrawable(resources, R.drawable.selected_task, null)
        draw?.setColorFilter(task.color.aRGB.toInt(), PorterDuff.Mode.OVERLAY)
        view.background = draw
        view.elevation = 8F

(I code in Kotlin) The drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="@color/select_border"/>
    <solid
        android:color="@color/select_fill"/>
</shape>

and the colors values

<color name="select_border">#78000000</color>
<color name="select_fill">#32000000</color>

The problem is when I select one item, the drawable is nicly applied but the shadows are tottaly removed and not amplified ! And if I not set the background with the drawable, it's works well. The shadows are amplified like expected.

What is the problem ?

doums
  • 470
  • 10
  • 24
  • Seems like a duplicate of [this](https://stackoverflow.com/questions/32286229/elevation-in-a-view-with-transparent-background). – azizbekian Apr 12 '18 at 06:59

1 Answers1

4

It is trouble from 2014 https://issuetracker.google.com/issues/37008403


Decision 1

Do not use colors with transparency for background CardView


Decision 2

Set background for View or ViewGroup inside the CardView instead of this cardView, then everything works correctly

enter image description here

serg3z
  • 1,852
  • 12
  • 28