androidandroid-recyclerviewandroid-cardview

CardView background color always white


I use RecyclerView with GridLayoutManager and have each item as CardView.

Unfortunately, the CardView here does not seem to change its background color. I tried in layout and programmatically as well but I have tried nothing seems to work.

I have been struggling for quite a while. I would appreciate it if someone could help me out with this issue.


Solution

  • If you want to change the card background color, use:

    app:cardBackgroundColor="@somecolor"
    

    like this:

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white">
    
    </android.support.v7.widget.CardView>
    

    Edit: As pointed by @imposible, you need to include

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    in your root XML tag in order to make this snippet function