Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cardview elevation is not working with androidx artifacts

My card view elevation stopped working and showing shadow after I updated my android studio 3.3 and migrating my project to androidx artifacts.

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="@dimen/card_margin"
        app:cardCornerRadius="@dimen/card_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
               <!--Other Code-->
        </LinearLayout>
    </androidx.cardview.widget.CardView>
like image 401
user10945815 Avatar asked Jan 21 '19 16:01

user10945815


People also ask

What is Androidx CardView?

androidx.cardview.widget.CardView. A FrameLayout with a rounded corner background and shadow. CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms.

What is CardView elevation?

Android 7.0 introduces a new widget called CardView which essentially can be thought of as a FrameLayout with rounded corners and shadow based on its elevation. Note that a CardView wraps a layout and will often be the container used in a layout for each item within a ListView or RecyclerView.

How do I customize my CardView?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

How padding is CardView in Android?

If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true) , or the XML variant cardUseCompatPadding="true" .


2 Answers

Add app:cardElevation="10dp" property to cardview.

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="@dimen/card_margin"
    **app:cardElevation="@dimen/card_margin"**
    app:cardCornerRadius="@dimen/card_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
           <!--Other Code-->
    </LinearLayout>
</androidx.cardview.widget.CardView>
like image 172
Dashesh Avatar answered Sep 30 '22 06:09

Dashesh


you need to specify app:cardUseCompatPadding="true" in your CardView then set app:cardElevation="4dp" with approbate value

  <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="12dp"
        app:cardCornerRadius="12dp"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp">
like image 37
Mohamed AbdelraZek Avatar answered Sep 30 '22 05:09

Mohamed AbdelraZek