changing the background of a card (Android cardView) - android

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">

Related

How to adapt images with CardView?

I am creating an App Gallery using CardView as my widget, but I am facing a problem that the images in my gallery are ignoring the size that I want for them, so they are all overlapping instead of creating a margin for it.
I am adapting the following Android Image Gallery:
https://medium.com/#moforemmanuel/android-simple-image-gallery-30c0f00abe64
I want something like that:
But running my code I get this result when I have more than one image:
And the code that I have based myself on you can see below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="190dp"
android:layout_height="190dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:layout_marginLeft=".5dp"
android:layout_marginRight=".5dp"
android:layout_marginBottom=".5dp"
app:cardCornerRadius="1dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
tools:ignore="ContentDescription" />
</FrameLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
What should I change in my code so that I can modify the images' size and still maintain a margin so that I will always have an adaptive layout?
Many thanks!
P.S.:
I've tried what Dharmaraj suggested but then I got the following layout:
Changing android:layout_width and android:layout_height to "200sp", it stills overlaps as you can see below:
To load the images I am using this two functions:
Try out this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_marginTop="1sp"
android:layout_marginLeft="1sp"
android:layout_marginRight="1sp"
android:layout_marginBottom="1Sp"
app:cardCornerRadius="1sp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_center="true" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
I've basically replaced FrameLayout with RelativeLayout for ease and please assign a constant height and width to the card view. Don't assign a constant height and width to the main layout. This fixed this problem for me. Please let me know if you need more clarification in the comments.
By the way the attribute android:src in image is missing. Or are you assigning image dynamically?

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

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">

Android, fit an image and textview into the screen

I added a fragment with an ImageView, which is a PNG image, and a TextView just below it.
The fragment is showing fine but when I rotate the screen the image will take most of the screen and the TextView is partially cut.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/shopping_cart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#id/shopping_cart"
android:text="#string/no_data"
android:gravity="center"/>
</RelativeLayout>
I'm trying to find a way to resize the image in order to show both image and text in the screen
try using a scroll-view as the root element if u want to retain the same image size rather than resizing the image. Here's a small snippet -
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(all your view elements)
</RelativeLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/text_image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
(Enter view here)
</LinearLayout>
Alright, so firstly ensure that you have the image for the ImageView in all densities. I would suggest that you use this attribute in your ImageView code:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
The scale type defines how your image should scale according to the view. I faced a similar issue once, and i was able to solve it using this. Having said that, i have not seen the image you are using, so i cannot be very sure of it. But, i would say that you use:
android:scaleType="centerCrop"
you can find the definition of the attribute on the link provided above. Also, i do not know what devices you are targeting for your app, but its always good to keep views inside a scroll view so that they are visible even on a smaller screen.
Give it a try, if not by centerCrop, then maybe by some other attribute.
Cheers!!
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/shopping_text"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/no_data"/>
<ImageView
android:id="#+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/shopping_text"
android:src="#drawable/shopping_cart"/>
</RelativeLayout>

Android: why are CardView margins not showing white background?

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.

Categories

Resources