Search code examples
androidxmlandroid-layoutscrollviewandroid-cardview

xml-layout: ScrollView cuts the bottom of an embedded CardView item


I have a single CardView inside a ScrollView that works flawlessly, as long as the height of the CardView is small enough not to need scrolling. However, if the CardView needs scrolling (in order to display a bigger image), the bottom of the CardView gets cut. What am I doing wrong?

Here's part of my layout.xml-file:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fillViewport="true" >

<android.support.v7.widget.CardView 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:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:elevation="8dp"
    app:cardCornerRadius="8dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_yellow_lighten_4"
        android:nestedScrollingEnabled="false"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView_downloaded"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:scaleType="fitCenter"
            android:src="@android:drawable/ic_menu_gallery"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <!-- several other TextView elements here not shown -->

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>


Solution

  • Here is the solution I've found:

    <android.support.v7.widget.CardView 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:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        app:cardCornerRadius="8dp"
        app:cardUseCompatPadding="true"
        >
    
    1. Remove android:layout_margin="8dp" from the CardView

    2. Add app:card_view:cardUseCompatPadding="true" to the CardView