Android: why are CardView margins not showing white background? - android

I have set up a CardView in a RecyclerView list. The layout_marginTop and layout_marginBottom are showing a grey color rather than the white background I had hoped to see (image below). Any ideas on how to get the top and bottom margins to show a white background?
<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/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="6dp"
android:layout_margin="4dp">
<TextView
android:id="#+id/cardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#android:color/white" />
</android.support.v7.widget.CardView>

Removed the TextView background and added this background to the CardView:
"card_view:cardBackgroundColor="some color""

Add some margin to card
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
app:cardCornerRadius="2dp"
app:cardElevation="4dp">
</android.support.v7.widget.CardView>

Make sure you're actually populating it with data from your domain model, i.e. check that you've implemented <data> and <variable> tags and that you've set your android:text="#{}" to the appropriate object.

Related

CardView for Android 9.0 has space black blank, how to remove the black space

CardView for Android has black spaces, how to remove it. i need help.
this is java or android problem.
enter image description here
<android.support.v7.widget.CardView
android:id="#+id/cv_result_knowledge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ll_result_header"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:visibility="gone"
app:cardBackgroundColor="#color/black_transparent40"
app:cardCornerRadius="#dimen/d12"
app:cardElevation="#dimen/d1"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/d10"
android:layout_marginTop="#dimen/d2">
</RelativeLayout>
</android.support.v7.widget.CardView>
Its because you set the background color to black, your Relative Layout has margin set so the black card background is visible in the margin area. Just remove the card background or remove margins, whichever suits you.
<android.support.v7.widget.CardView
android:id="#+id/cv_result_knowledge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ll_result_header"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:visibility="gone"
app:cardBackgroundColor="#color/black_transparent40" -<REMOVE THIS
app:cardCornerRadius="#dimen/d12"
app:cardElevation="#dimen/d1"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/d10" -<REMOVE THIS
android:layout_marginTop="#dimen/d2"> -<REMOVE THIS

Centering cards in RecyclerView

The question is pretty straightforward, I've a CardView in a RecyclerView using a GridLayout with 2 columns.
The colors are just to make easy to see the components...
white is the LinearLayout
blue is the RecyclerView
green is the CardView
I want the cards be centered in the view with some margin from the edge... but i couldn't achieve it doesn't matter what I do. (this is what I want, made on paint)
If it is not possible, i would be okay with the cards aligned to the edge with all margin left between then
HERE GOES THE CODE:
This is the CardView:
<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/cardView"
android:layout_width="#dimen/influencerCardSize"
android:layout_height="#dimen/influencerCardSize"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/smallMargin"
card_view:cardBackgroundColor="#android:color/holo_green_dark"
card_view:cardElevation="0dp"
card_view:layout_anchorGravity="center_horizontal">
This is the RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="#dimen/influencersViewHeight"
android:layout_gravity="center"
android:layout_margin="#dimen/smallMargin"
android:background="#android:color/holo_blue_bright"
android:scrollbars="vertical"
android:visibility="visible" />
Try having you card view inside the Linear layout
Like :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_margin="10dp"
android:id="#+id/cardView"
card_view:cardBackgroundColor="#android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="150dp"
</android.support.v7.widget.CardView>
</LinearLayout>
Your RecyclerView looks good.
Then if you want only two card per row .. try using AutoFitGridLayoutManager for the recycler view
Like:
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(context, width);
recyclerView.setLayoutManager(layoutManager)

How to add Image without this white background?

enter image description here
Here it is a XML file,
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="#fff2f2"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="12dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/tick"
android:id="#+id/image2"/>
</LinearLayout>
And Mainactivity.java,
item2List.add(new Items2(R.drawable.checked));
The default color of CardView is White. If you want to change it to some other color use following code.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/white">
</android.support.v7.widget.CardView>
or you can set cardBackgroundColor to transparent(if you don't want any color) by setting cardBackgroundColor as
app:cardBackgroundColor="#android:color/transparent"
Set app:cardBackgroundColor="#android:color/transparent" instead of app:cardBackgroundColor="#fff2f2".
This will take background of your Image rather than force white background.
Here, I have no idea about your image is .png or .jpeg, but if your image is not .png then take .png image and if you have already taken .png image than set cardview background transparent.
Here, use the cardBackgroundColor attribute to remove color and set transparent and cardElevation attribute to remove the drop shadow.
Please try below:-
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardElevation="0dp"
app:cardBackgroundColor="#android:color/transparent" >
If you you use API level 21 and higher and in case of cardBackgroundColor not work properly you can use android:backgroundTint. In my case it works.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardElevation="0dp"
android:backgroundTint="#android:color/transparent">

changing the background of a card (Android cardView)

I am having a tough time changing the background color of a card in Android (cardView).
I did this in the XML:
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_margin="5dp"
android:layout_gravity="bottom"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp"
card_view:cardBackgroundColor="#2600e622">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
But it did not work. I keeps defaulting to the white back ground color.
I am not sure where to go from here.
Any ideas?
Also - I want to position the card in the center of the screen and the card ends up being in the center but at the TOP of the screen.
Can I get some insight on how to position the card in the center of the screen as well?
Thanks.
This should works.
<?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/card_view"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="5dp"
android:layout_gravity="center"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp"
card_view:cardBackgroundColor="#00490b">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// add more views here
</RelativeLayout>
</android.support.v7.widget.CardView>
I used this code to set programmatically:
card.setCardBackgroundColor(Color.WHITE);
Or in XML you can use this code:
card_view:cardBackgroundColor="#android:color/white"
if you have in your colors.xml this color for example:
<color name="ambar400">#FFCA28</color>
in your class use:
card_name.setCardBackgroundColor(getResources().getColor(R.color.ambar400));
Set the color to your relative layout background if he is the root layout of your cardview:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:colorBackground="#2600e622">

Transparent background on CardView - Android

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
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);
in SDK version 21 or higher steps to make Android CardView transparent.
Set android:backgroundTint="#android:color/transparent". This is CardView attribute to set background.
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" />
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"
>
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>
This should work on API 17
cardView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));
Just add background color app:cardBackgroundColor="#0000"
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#0000">
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>

Categories

Resources