My Cardview inside Listview is not showing shadow in Android L(Nexus 5). Also the round edges are not properly shown. Here is the code for Listview\'s Adapter View :
In my case the shadow was not showing on Android L devices because I did not add a margin. The problem is that the CardView creates this margin automatically on <5 devices so now I do it like this:
CardView card = new CardView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if (Build.VERSION_CODES.LOLLIPOP == Build.VERSION.SDK_INT) {
params.setMargins(shadowSize, shadowSize, shadowSize,
shadowSize);
} else {
card.setMaxCardElevation(shadowSize);
}
card.setCardElevation(shadowSize);
card.setLayoutParams(params);
Simply add this tags:
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
So its become:
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="?colorPrimary"
app:cardCornerRadius="3dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="1dp" />
You can try by adding this line
card_view:cardUseCompatPadding="true"
The Whole code will seems 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:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp">
</android.support.v7.widget.CardView
use app:cardUseCompatPadding="true"
inside your cardview.
For Example
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/cardviewMarginRight"
app:cardBackgroundColor="@color/menudetailsbgcolor"
app:cardCornerRadius="@dimen/cardCornerRadius"
app:cardUseCompatPadding="true"
app:elevation="0dp">
</android.support.v7.widget.CardView>
Add android:hardwareAccelerated="true" in your manifests file like below it works for me
<activity
android:name=".activity.MyCardViewActivity"
android:hardwareAccelerated="true"></activity>
Add this line to CardView....
card_view:cardUseCompatPadding="true" //for enable shadow
card_view:cardElevation="9dp" // this for how much shadow you want to show
Tips
You can avoid layout_marginTop and layout_marginBottom as shadow itself takes some space to the up and down of it.The amount space defined by how much you will use in card_view:cardElevation="ndp" .
Happy Coding (: