Home > Software engineering >  Background color of checkable cardview is applying an alpha of primaryColor
Background color of checkable cardview is applying an alpha of primaryColor

Time:12-09

I was trying to create a checkable card view and the code is like

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        findViewById<MaterialCardView>(R.id.card).setOnClickListener {
            findViewById<MaterialCardView>(R.id.card).isChecked = !findViewById<MaterialCardView>(R.id.card).isChecked
        }
    }
}

The Layout is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:id="@ id/card"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        app:cardBackgroundColor="@color/background_color"
        app:checkedIcon="@null">
    </com.google.android.material.card.MaterialCardView>
</LinearLayout>

background_color is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:state_enabled="true" android:state_checked="true"/>
    <item android:color="@android:color/white" android:state_enabled="true" android:state_checked="false"/>
    <item android:color="@android:color/black" />
</selector>

It seems functionally working. However, when the status is checked its not 100% black. Somehow it applies an alpha of primary color.

enter image description here

Its hard to see, but it clearly shows if I apply exposure manually with photoEditor.

enter image description here

How can I make it pure black or avoid that unexpected alpha?

CodePudding user response:

I found the solution. The following attribute in cardview will stop adding that alpha.

app:cardForegroundColor="@null"
  • Related