Search code examples
androidandroid-layout

Using nestedscrollview doesn't align under a textview using constraintlayout


I have the following layout

enter image description here

Seems ok in the preview window.

But when I launch the bottomsheet it overlaps the view above it. I am not sure if this issue with the nestedScrollview. I am using the NestScrollView because the user can scroll the bottomsheet to move it up, when it first opens. If I just use a plan ScrollView then the content will not scroll and will still displays incorrectly.

enter image description here

I don't understand this, this is my xml layout

<?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="wrap_content"
    android:background="@drawable/bg_bottom_sheet_sort_filter">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_bottom_sheet_sort_filter"
        android:elevation="@dimen/margin_2dp"
        android:translationZ="@dimen/margin_2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_offer_tv"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/margin_60dp"
            android:gravity="center"
            android:letterSpacing="0.03"
            tools:text="Get ฿1,000 off"
            android:textColor="@color/black"
            android:textSize="@dimen/text_large"
            app:fontFamily="@font/cpn_bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/close_icon"
            android:layout_width="@dimen/margin_22dp"
            android:layout_height="@dimen/margin_22dp"
            android:layout_marginEnd="@dimen/margin_16dp"
            android:background="@drawable/close_icon_terms"
            app:layout_constraintBottom_toBottomOf="@id/coupon_offer_tv"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/coupon_offer_tv" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/coupan_terms_and_cond_bg"
        android:paddingHorizontal="@dimen/margin_16dp"
        android:paddingTop="@dimen/margin_24dp"
        android:paddingBottom="@dimen/margin_24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/cardview">

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_code_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:letterSpacing="0.01"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/text_medium"
            android:textStyle="bold"
            app:fontFamily="@font/cpn_bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:textAllCaps="false"
            tools:text="This is the coupon TV Code"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/coupon_desc_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/text_small"
            app:fontFamily="@font/cpn_regular"
            app:layout_constraintStart_toStartOf="@+id/coupon_code_tv"
            app:layout_constraintTop_toBottomOf="@+id/coupon_code_tv"
            app:textAllCaps="false"
            tools:text="Coupon description that describes the coupons"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/copy_coupon_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:padding="@dimen/margin_14dp"
            tools:text="Copy"
            android:textColor="@color/white"
            android:textSize="@dimen/text_vlarge"
            app:fontFamily="@font/bebasneue_regular"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/coupon_code_tv"
            app:textAllCaps="true" />

        <androidx.core.widget.NestedScrollView
            android:layout_width="@dimen/margin_0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_14dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/copy_coupon_tv">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/terms_and_condition_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:includeFontPadding="false"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="These are the rules we must follow" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/coupon_brand_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/terms_and_condition_rules_tv"
                    tools:text="Aesop, Amazfit, Apple, Beats, Bodum, Cellreturn, Chanel, Garmin, Genedia, Gift Card, Gopro, Hatari, Lenovo, Lets Clean, Lg, Marshall, MI, Muji, Nintendo, Nt, Pasaya, Radley , Reebok, Samsung, Sharp, Sony, Suunto, Toshiba, Vivo, Xfit, Xiaomi, Zebra" />

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/coupon_product_rules_tv"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:lineSpacingExtra="-2dp"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_msmall"
                    android:visibility="visible"
                    app:fontFamily="@font/cpn_regular"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/coupon_brand_rules_tv"
                    tools:text="@tools:sample/lorem/random"/>

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Try setting the layout_height property of your NestedScrollView to match_constraint. Also, you can set the default height to wrap so that it only takes only the space it needs.

    <androidx.core.widget.NestedScrollView
        android:layout_width="@dimen/margin_0dp"
        android:layout_height="0dp" //<--- Match constraint
        android:layout_marginTop="@dimen/margin_14dp"
        android:fillViewport="true"
        app:layout_constraintHeight_default="wrap" //<-- Take only the amount of space required
        app:layout_constraintVertical_bias="0" //<-- This will force the view to align to the top
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/copy_coupon_tv">