I'm trying to implement simple cardview with semi-transparent background.
<android.support.v7.widget.CardView
android:id="#+id/card_view"
app:cardBackgroundColor="#33FFFFFF"
app:cardCornerRadius="4dp"
android:foreground="#33FFFFFF"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="100dp">
......
</android.support.v7.widget.CardView>
And this is the result in L and preL
android L: (Nexus 5)
Pre L:
Any idea how to fix the pre L to look like L ?
As you can see there are too many borders/strokes surrounding the card.
I'm can guess it relates to shadows and stuff...
Thanks.
Look like it's a known defect in google forums
Thanks to #benhylau for providing implementation for cardview with alpha here
There no possible to color cardview background with #__FFFFF as it ignores the transparency values. need to work with alpha attribute
Related
Hello can you tell me how can I add the rectangular box in the android layout which adds up like a 3d effect as in the below screenshot. Thanks.
You can add this effect to almost every view or widget, using android:elevation attribute, or programmatically using setElevation(x) method. X is number of Dp.
Note: This only works on api level 21 or newer.
you are referring to material design you can add this effect using card view
Add elevation to your view
read this documentation for assigning elevation to your view
https://developer.android.com/training/material/shadows-clipping.html
Add dependency compile 'com.android.support:cardview-v7:23.0.1'
<android.support.v7.widget.CardView
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/grey_300"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
This is my card_view. I have mentioned card_view:cardElevation.
But still shadow is not showing.
I have searched many links. Everywhere they have mentioned to use card_view:cardElevation.
<android.support.v7.widget.CardView
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="4dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false">
</android.support.v7.widget.CardView>
Can you tell what I am missing here?
Read https://developer.android.com/training/material/lists-cards.html
To create a card with a shadow, use the card_view:cardElevation
attribute. CardView uses real elevation and dynamic shadows on Android
5.0 (API level 21) and above and falls back to a programmatic shadow implementation on earlier versions. For more information, see
Maintaining
Compatibility.
UPDATE
try adding margin to card if you want to see shadow
check https://developer.android.com/reference/android/support/v7/widget/CardView.html
Set the following attribute in your cardview
card_view:cardUseCompatPadding = "true"
card_view:cardElevation = "5dp"
Following code works perfectly on Kitkat, but shadows are not visible in Lollipop.
Actually I can see shadow in Android Studio Preview, but not while running on a device/emulator. I'm using CardView for adapter of ViewPager (android.support.v4.view.ViewPager)
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp">
Actually it also shows shadow in L version, but it is based on elevation so you can't see the shadow if card height is match parent
try adding margin to card if you want to see shadow
Found the solution. I'm able to get the shadow effect using following code.
android:background="#android:drawable/dialog_holo_dark_frame"
I have an app that runs well in any version of android, but in Lollipop seems that all views don't respect any margin or padding.
Does anyone know why??
Thanks in advance.
I'm declaring the CardView like this:
<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_view1"
android:background="#color/backgroundItem"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
card_view:cardCornerRadius="0dp"
android:layout_weight="1">
Per the CardView documentation:
Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.
I've checked a whole bunch of answers here, and still no dice. Android Studio preview shows the shadow, but not the 5.1.1 Nexus 7.
Here's my XML
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="20sp"
card_view:cardPreventCornerOverlap="true"
android:layout_margin="6dp"
>
So I've used cardUseCompatPadding (also here), checked the documentation, made sure the card has a margin .... no dice.
I posted the total XML file over at https://gist.github.com/tigerhawkvok/0ca3d1f402afa29642d5 .
Help?
On device, shadows are drawn only if the view background is not null and not transparent. However, a bug in the layout preview in Android Studio prevents it from checking the background before drawing the shadow. Hence you see the difference.
But you shouldn't really need to set the elevation on card view, since it creates its own shadow or sets the elevation depending on the platform version.