my android version is 4.2 and i'm trying to remove white border and space from that but my solution as card_view:cardPreventCornerOverlap="false" from xmlns:app="http://schemas.android.com/apk/res-auto" namespace doesnt work correctly
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="0dp"
card_view:cardPreventCornerOverlap="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/market_item_background">
<ImageView
android:id="#+id/market_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"/>
</RelativeLayout>
this is part of my application which i used CardView
Try setting
setCardElevation(0);
setMaxCardElevation(0);
and
card_view:cardPreventCornerOverlap="false"
Source: Cardview - white border around card
After much research only thing work for me with pre lollipop devices
with using both
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
in CardView,
this is a old question but might help somebody
Replace android:layout_margin="10dp"
for android:layout_padding="10dp"
I think that setting setMaxCardElevation(0) will be enough in that case.
No need in setCardElevation(0) or card_view:cardPreventCornerOverlap="false"
I set card background color to transparent and it makes the white border disappear
app:cardBackgroundColor="#android:color/transparent"
Related
This question already has answers here:
White Space with CardView on pre-lollipop devices
(2 answers)
Closed 3 years ago.
When i run my code on api 28 i do not see this white border along the inner edges of the cardview. but on api 19 i get the following rendered:
My desire is for the color to entirely fill the cardview, and it should clip the corners so i keep the desired rounded effect. here is the simple demo code showing its not working:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="300dp"
android:layout_height="170dp"
android:theme="#style/Theme.MaterialComponents.Light"
app:cardBackgroundColor="#android:color/white"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:rippleColor="#android:color/transparent"
app:strokeWidth="0dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark" />
</com.google.android.material.card.MaterialCardView>
note: take a look at what it looks like when rendered on api 28, its exactly what i desire:
UPDATE: I ADJUSTED
using app:cardPreventCornerOverlap="false" , but makes it not rounded. when i put a border around it its not contained in the border now.
can someone suggest :
Just set app:cardPreventCornerOverlap="false" in your xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="300dp"
android:layout_height="170dp"
android:theme="#style/Theme.MaterialComponents.Light"
app:cardBackgroundColor="#android:color/white"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:rippleColor="#android:color/transparent"
app:strokeWidth="0dp"
app:cardPreventCornerOverlap="false" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark" />
</com.google.android.material.card.MaterialCardView>
i ended up creating a white border that i use ..so i set the strokeColor to white if its below api 19. that seems to be the best i can do . and i do set app:cardPreventCornerOverlap="true" ...its not perfect but achieving elevation on lolipop is a dream come true with this materialCardView.
I presume, this is quite a basic question but I had to ask it because of my attempt for finding relevant answers for setting white glow on MaterialCardView or simply CardView goes in vain, They were offering setting border color instead, now anyone please bother yourself to enlighten me with the proper solution.
I have a green glow around white cardview
Try this code
<android.support.v7.widget.CardView
card_view:cardBackgroundColor="#color/green"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<android.support.v7.widget.CardView
android:layout_margin="3dp"
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/white"
card_view:cardCornerRadius="10dp"
>
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
Use 9-patch image like this one:
and apply it to a FrameLayout, then put your content with white background inside
Or for API 28+ you can try using android:outlineSpotShadowColor="#5500FF00" attribute
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">
So i am having this layout file (below). As you can see, there is no padding or margin. The dimen.xml files also dont have any padding/margin. Finally, i do NOT change the layout programmatically at all.
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:padding="0dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/toolbarholder"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="16dp"
app:cardCornerRadius="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
</android.support.v7.widget.CardView>
<ListView
android:layout_below="#+id/toolbarholder"
android:layout_above="#+id/cardroot"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="0dp" />
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardroot"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/transparentblack"
app:cardCornerRadius="0dp"
app:cardElevation="16dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttonholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:weightSum="3">
<ImageView
android:id="#+id/previous"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/previous"
android:layout_weight="1"/>
<ImageView
android:id="#+id/play"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/play"
android:layout_weight="1"/>
<ImageView
android:id="#+id/next"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/next"
android:layout_weight="1"/>
</LinearLayout>
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonholder" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Below are two screenshots from two different devices.
Lenovo k3 Note:
HTC Desire 550:
Any ideas what might be the cause of this?
Ok Guys... thanks for your input. Turns out the "Cardview Documentation" mentions that:
Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.
This means that the Margin is intended behavior to draw the shadows and simulate elevation. One possible solution for everyone having the same problem, is use the attribute cardUseCompatPadding in your layout as follows:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>
This will cause your layout to be "rendered" the same way (the pre-Lollipop way //sad ) in every device regardless of the API. At least this way we can fix a common layout that works for all versions given this restriction.
Hope it helps.
UPDATE: If you decide to go for "app:cardUseCompatPadding=true" here is another advice. Use negative layout_margin, to balance-out the unwanted padding from the compat library. Example below:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="-10dp"
app:cardCornerRadius="0dp"
app:cardUseCompatPadding="true"
app:cardElevation="8dp">
</android.support.v7.widget.CardView>
This way your layout can get rid of unwanted padding AND look the same pre-lollipop and after lollipop.
Hope this helps even more. :)
The possible reason is that well HTC mobiles especially have different types of internal layout files. So Lenovo Nexus and SAMSUNG may be functioning properly but thanks to HTC's difference in these situations it is a big problem for lot of developers. If you really want it to work on HTC as well I recommend the best way to make it work on HTC is to create your own xml files which resemble the android support ones. I recommend if you want to proceed you try to get a hang of the native xml file of the android card one and use the same. Now HTC has to use your custom ones which are equal to all other android ones but not in HTC. What I am saying is that now It will work properly on all devices. But be warned, this just might turn out to be long and painful if you can't get a hang of the native xml file. If you do it'll probably then become a breeze for you. I hope you understand me.
Have you tried setting the height for your linear layout or set it to match parent instead? It doesn't seem like there is extra padding but rather that your items in the list are not the same height as the image even gets Cut off.
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>