DVT
DVT

Reputation: 165

CardView background color appears red in API 21

I have a CardView that changes cardBackgroundColor using a selector drawable depending on its activated state. This works fine in API 25 and 30 emulators, but in an API 21 emulator the background color is red and doesn't change. I don't have any custom colors that are red.

CardView

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="6dp"
android:layout_marginTop="8dp"
app:cardBackgroundColor="@drawable/list_item_note_background"
app:cardCornerRadius="4dp">

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/note_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp" />

</androidx.cardview.widget.CardView>

Drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_activated="true" />
<item android:color="?attr/colorSurface" />
</selector>

Primary color is #04d4f0 Surface color is #FFFFFF

Upvotes: 0

Views: 211

Answers (1)

Neha Rathore
Neha Rathore

Reputation: 427

problem is with your drawable, I hope colorPrimary and colorSurface already there in your colors.xml file , Here you are accessing color via attrs which will pick android os provided color use below drawable hope that work for you.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_activated="true" />
    <item android:color="@color/colorSurface" />
</selector>

Upvotes: 2

Related Questions