'Drawable as Background for CardView
the CardView ( android.support.v7.cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. No Idea what I am doing wrong here.
Solution 1:[1]
I know this is an old question, but I have a simple solution - just make the first child of your CardView an ImageView and specify the scale type to fitXY. You can get rid of the extra CardView padding by setting cardElevation and cardMaxElevation to 0dp:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="200dp"
app:cardCornerRadius="4dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp">
<ImageView
android:src="@drawable/your_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<... your layout
.../>
</android.support.v7.widget.CardView>
Solution 2:[2]
for drawable or color just use:
cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
for color use:
cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);
but this one does not produce the intended results
Solution 3:[3]
Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/list_container_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
Solution 4:[4]
I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview.
<android.support.v7.widget.CardView
android:id="@+id/result_cv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
app:cardCornerRadius="16dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
android:gravity="center"
>
<!-- your views -->
</LinearLayout>
</android.support.v7.widget.CardView>
Solution 5:[5]
The command I used was:
cardView.setBackgroundResource(R.drawable.card_view_bg);
where card_view_bg is a custom XML resource file.
If you have some layouts inside the card view and these layouts are overlapping with the margins of the card view then you might need a separate custom background resource file for the layouts like the one I used for the card view background itself.
Solution 6:[6]
Try this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/list_container_bg"
android:layout_width="match_parent"
android:background="@drawable/yourbackground"
android:layout_height="match_parent">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
card_view:cardBackgroundColor="@android:color/holo_green_dark
here you can set any color or make it transparent by giving color transparent and if u wand some drawable like image or svg put RelativeLayout background
Solution 7:[7]
You can use LinearLayout or Relative layout inside CardView and set drawable background it like below :
<android.support.v7.widget.CardView
android:id="@+id/card6"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/card4"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:cardMaxElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/card_bg"
android:padding="10dp">
<ImageView
android:id="@+id/card6Image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/card6Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="Daily Check In"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="@+id/card6Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Just Check in Daily and Earn Credits"
android:textColor="#ffffff"
android:textSize="10sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Below is my Drawable resource file :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
Solution 8:[8]
you can set foreground as follow for the card view to set custom bg
android:foreground="@drawable/bg"
and here is the transparent bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#d9d9d9" />
<corners android:radius="4dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
Solution 9:[9]
You have 2 possible solutions.
- Setting background programmatically:
cardView.setBackgroundResource(R.drawable.background)
- Add some child to cardview that will fill cardview's bounds and set background on it:
<android.support.v7.widget.CardView ...> <ConstraintLayout android:background="@drawable/background" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.v7.widget.CardView>
This way you can also give images some shape that you can configure by the shape of cardview. Disadvantage is that it is bad for performance.
Solution 10:[10]
Try this code:
cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
Solution 11:[11]
sCard.setBackgroundResource(R.drawable.scard_background);
Solution 12:[12]
if you want change background color on cardView just add this code on your cardview XML
app:cardBackgroundColor="@color/whatever_color_you"
Solution 13:[13]
You can simply use the below line in your xml file.
app:cardBackgroundColor="@drawable/your_custom_theme_here"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
