CardView bottom border is cut off inside ScrollView android
Setting clipToPadding
to false
on a ScrollView
usually works for me:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:contentPadding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Lorem ipsum..." />
</com.google.android.material.card.MaterialCardView>
</ScrollView>
One solution I just found is to wrap CardView with a LinearLayout or RelativeLayout and set its padding. For example, If you want some drop shadow effect in cardView, lets say 8dp, you can set 4dp padding of your LinearLayout or RelativeLayout and 4dp layout_margin of CardView.
UPDATED
This will work better now with a NestedScrollView
and a MaterialCardView
. I added this NestedScrollView
to a ConstraintLayout
.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true">
This is working for me without the LinearLayout wrapper.
===========================================================================
OLD WAY LEFT HERE
I ran into the same problem and had to do the following (the key is the LinearLayout wrapper around the cardview where I added the paddingBottom):
<ScrollView
android:id="@+id/item_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
tools:visibility="visible">
<LinearLayout
android:id="@+id/item_wrapper_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/content_margin"
android:paddingBottom="32dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/item_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/colorPrimary"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
Adding the LinearLayout wrapper around the cardview is what worked for me.
Also note, I had to add card_view:cardUseCompatPadding="true" on the cardview to get the border shadow looking correct.
Here is the end result where the red box shows where the padding has been added when the cardview is expanded and scrolled up.
In my case I just have to change ScrollView
with NestedScrollView
to solve the problem.
FYI, my NestedScrollView
is placed inside a fragment which is a child of CoordinatorLayout
with appbar_scrolling_view_behavior
set.