0

My layout file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    tools:context=".TurnOnLocation">

    <LinearLayout
        android:id="@+id/requires_location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/turn_on_location">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="4dp"
            app:cardElevation="10dp"
            app:cardPreventCornerOverlap="false">
            <TextView
                android:id="@+id/info_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:paddingStart="60dp"
                android:paddingEnd="60dp"
                android:textSize="28sp"
                android:text="test"
                android:textColor="@color/colorPrimary"
                android:textAlignment="center"
                android:background="@color/colorPrimaryDark" />

        </androidx.cardview.widget.CardView>
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/turn_on_location"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/medium_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/turn_on_location"
        app:layout_constraintTop_toBottomOf="@id/requires_location"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I've specified the app:cardElevation="10dp" which I believe is meant to render the drop shadow. However no shadow is appearing.

Any idea why?

Zorgan
  • 8,227
  • 23
  • 106
  • 207

5 Answers5

2

Add this line to your AndroidManifest this android:hardwareAccelerated="true" in application tag

James Christian Kaguo
  • 1,251
  • 15
  • 14
2

You've no mistake in your source code. The shadow should be rendered correctly. You might be checking the output in your layout preview. It has some issue on rendering. Please run the app in emulator or real device.

theapache64
  • 10,926
  • 9
  • 65
  • 108
2

Use this Code, for me its works! the idea is on app:cardUserCompatPadding="true"

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="4dp"
    android:layout_marginVertical="@dimen/two_x"
    app:cardBackgroundColor="@color/white">

Result

enter image description here

Yohanim
  • 3,319
  • 9
  • 52
  • 92
0

add below lines into CardView tag

  card_view:cardBackgroundColor="@android:color/white"
  card_view:cardElevation="2dp"
  card_view:cardUseCompatPadding="true"
Arwy Shelke
  • 333
  • 1
  • 2
  • 12
  • Doesn't add a shadow. It also prevents me from having 0 padding horizontally: https://i.imgur.com/ab3Uo1l.png – Zorgan May 13 '19 at 09:42
0

Try this

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        app:cardElevation="10dp"
        android:layout_margin="16dp">
</androidx.cardview.widget.CardView
Ajay Gohel
  • 243
  • 7
  • 17