How to set semi transparent background color for Android CardView?
Asked Answered
P

2

0

I want to set the backgroundColor of a card view with an argb color. But the result is like this:

enter image description here

<androidx.cardview.widget.CardView
        android:id="@+id/infoCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:cardBackgroundColor="#59448AFF"
        app:cardCornerRadius="8dp">

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/space_16dp"
            android:layout_marginTop="@dimen/space_8dp"
            android:layout_marginEnd="@dimen/space_16dp"
            android:layout_marginBottom="@dimen/space_8dp"
            android:lineSpacingExtra="@dimen/space_4dp"
            android:text="This is a sample text"
            android:textColor="@color/secondaryText"
            android:textSize="16sp" />

    </androidx.cardview.widget.CardView>

Does anybody know how to fix this?

Putamen answered 8/4, 2020 at 23:43 Comment(0)
K
0

Define it within color.xml

<color name="semiTransparentColor">#20ffffff</color>

Now set the card view like this way.

card_view:cardBackgroundColor="@color/semiTransparentColor"

Here is the code of hexadecimal code of transparency. link you can set as you need.

Kuehn answered 9/4, 2020 at 0:5 Comment(0)
P
0

Just set this to a card view

app:cardElevation="0dp"

or try with removing the background of your com.google.android.material.textview.MaterialTextView

android:background="@null"
Pecoraro answered 23/9, 2021 at 13:44 Comment(1)
This answer could use an explanation of how setting the elevation relates to setting the background color.Halcomb

© 2022 - 2024 — McMap. All rights reserved.