androidxmlandroid-nestedscrollview

How do NestedScrollView works?


I am working on an Android application and I have a toolbar and below that I have a NestedScrollView and inside that some other views. I have done everything right but still, the contents are not scrolling properly.

Below is my XML code for the same.

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".activities.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primaryColor"
        android:elevation="3dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/titleTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginStart="8dp"
            android:padding="4dp"
            android:text="@string/home"
            android:textColor="@color/white"
            android:textSize="24sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/settingsIV"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:layout_gravity="end"
            android:layout_marginEnd="8dp"
            android:contentDescription="@string/settings_button"
            android:elevation="2dp"
            android:src="@drawable/ic_settings"
            app:tint="@color/white" />

    </androidx.appcompat.widget.Toolbar>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/homeSV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <LinearLayout
            android:id="@+id/svLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/searchLayout"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:background="@drawable/search_bg"
                android:elevation="3dp"
                android:orientation="horizontal">

                <androidx.appcompat.widget.SearchView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="4dp"
                    app:queryHint="@string/search_here" />

            </LinearLayout>

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/dailyProgressCV"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:backgroundTint="@color/borderColor">

                <LinearLayout
                    android:id="@+id/cvLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/headingTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:fontFamily="@font/amaranth"
                        android:text="@string/daily_progress"
                        android:textColor="@color/black"
                        android:textSize="28sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/subHeadingTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:fontFamily="@font/amaranth"
                        android:text="@string/track_your_daily_tasks_and_habits"
                        android:textColor="#595959"
                        android:textSize="20sp" />

                    <TextView
                        android:id="@+id/percentageTV"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="24dp"
                        android:fontFamily="@font/amaranth"
                        android:text="@string/_65"
                        android:textColor="@color/black"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <ProgressBar
                        android:id="@+id/progressBar"
                        style="?android:attr/progressBarStyleHorizontal"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="16dp"
                        android:progress="65"
                        android:progressBackgroundTint="@color/secondaryColor"
                        android:progressTint="@color/primaryColor" />

                </LinearLayout>

            </com.google.android.material.card.MaterialCardView>

            <TextView
                android:id="@+id/taskHeadingTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="20dp"
                android:fontFamily="@font/amaranth"
                android:padding="3dp"
                android:text="@string/task_categories"
                android:textColor="@color/black"
                android:textSize="26sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:orientation="horizontal"
                android:weightSum="1">

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginEnd="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginStart="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:orientation="horizontal"
                android:weightSum="1">

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginEnd="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginStart="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:orientation="horizontal"
                android:weightSum="1">

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginEnd="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginStart="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:orientation="horizontal"
                android:weightSum="1">

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginEnd="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

                <com.google.android.material.card.MaterialCardView
                    android:layout_width="0dp"
                    android:layout_height="120dp"
                    android:layout_marginStart="8dp"
                    android:layout_weight=".5">


                </com.google.android.material.card.MaterialCardView>

            </LinearLayout>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

And the the output I got is here, the last cards are not coming up while scrolling.

output screenshot


Solution

  • I see that your code had an ambiguity nested layout. Constraint layout is not recommended using fill match_parent, better change to match_constraints. And i see that your child inside constraint layout doesnt had contraints to another component.

    My recommendations, you can implementation Linear Layout rather than constraint_layout for simple user interface. And inside nested_scroll_view you can add attribute fill_view_port=true.