108

I want to do transparent background on CardView. I know backgroundColor but i have image on my Layout.

Do you know how do it? Or something which work as cardview but i will set a transparent background?

Regards

BSavaliya
  • 809
  • 1
  • 16
  • 26
mac229
  • 4,319
  • 5
  • 18
  • 24

8 Answers8

211

Setup your CardView to use the cardBackgroundColor attribute to remove color and cardElevation attribute to remove the drop shadow. For example:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myCardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardElevation="0dp"> 

For a full list of supported attributes see here: https://developer.android.com/reference/android/support/v7/widget/CardView.html

If you are using an older API, you will need to call these two functions on your CardView instead:

myCardView.setCardBackgroundColor(Color.TRANSPARENT);
myCardView.setCardElevation(0);
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
18

in SDK version 21 or higher steps to make Android CardView transparent.

  1. Set android:backgroundTint="@android:color/transparent". This is CardView attribute to set background.

  2. Set android:cardElevation="0dp" to remove the shadow.

For example, here is small xml code to create transparent CardView

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@android:color/transparent"
        app:cardElevation="0dp" />
Adarsh Dhakad
  • 347
  • 1
  • 3
  • 10
Rahul Raina
  • 3,322
  • 25
  • 30
8

In my case, I used the attribute android:backgroundTint="@color/some_color",it is only used en API level 21 and higher. And color #50000000 for example.

<android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="3dp"
        app:cardElevation="0dp"
        android:backgroundTint="@color/negro_label"
        >

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
geros
  • 81
  • 1
  • 2
4

use app:cardBackgroundColor="@android:color/transparent"

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    app:cardCornerRadius="16dp"
    app:cardElevation="16dp"
    app:cardBackgroundColor="@android:color/transparent" >

<--inside cardlayout-->

    </android.support.v7.widget.CardView>
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Kapil Parmar
  • 881
  • 8
  • 19
3

This should work on API 17

cardView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Cristian Cardoso
  • 665
  • 6
  • 11
1

Just add background color app:cardBackgroundColor="#0000"

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardBackgroundColor="#0000"> 
user12091113
  • 51
  • 1
  • 5
0

You must have cardView to make image circle shape

       <androidx.cardview.widget.CardView
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:cardCornerRadius="25dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:elevation="0dp"
            app:cardBackgroundColor="#00424242"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/play_icon"
                android:padding="5dp"
                android:background="#19000000"
                android:contentDescription="TODO" />

        </androidx.cardview.widget.CardView>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '22 at 14:44
0

What I wanted was to have a textview inside a cardview but I wanted the back of the cardview to be transparent. I solved it this way:

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:cardBackgroundColor="@color/semiTransparentColorr"
        app:cardUseCompatPadding="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/cardview_stroke"
            android:padding="16dp">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Gold"
                android:textSize="12sp" />


        </LinearLayout>
    </androidx.cardview.widget.CardView>

First you need to set the value of the background color of the cardview to #1AFFFFFF (semiTransparentColorr). Then we give cardview_stroke.xml as background to linearlayout. cardview_stroke.xml codes should be like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/semiTransparentColorr"/>
    <stroke android:width="1dp" android:color="@color/black"></stroke>
    <corners android:radius="8dp"/>
</shape>
Safak
  • 11
  • 4