android-layoutlayoutscrollviewandroidxcardview

Android : scroll view causing issue while displaying


I have a constraint layout which has 2 child's , the top most is the cardView (androidX) and the next one is the scrollView.

Normally it work's fine , but when the scroll view has to many children's (like the one shown below) the view's start causing issues. I can't seem to scroll to the top most child of scroll view nor the bottom most.

Thing's i have tried are :

  1. Change card view to linearLayout (doesn't solve the issue)
  2. add android:layout_marginTop="180dp" to the scroll view (hide's the last few child's)
  3. change the constraints to point to parent for scroll view (caused cardview to overlap on top of scrollview)

I want the scroll view to start just below the card view and all it's children's should be visible no matter the child count.

How can i achieve this??

Scenario 1

Scroll top

Scenario 2

scroll bottom

my code

<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"
    android:contentDescription="@string/accessory_page"
    android:padding="20dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/accessory"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="15dp"
            android:text="@string/blank"
            android:textAlignment="center"
            android:textSize="24sp"
            android:textStyle="bold|italic" />

    </androidx.cardview.widget.CardView>

    <ScrollView
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:importantForAccessibility="yes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView">

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

            <TextView
                android:id="@+id/acc456essory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="text1"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/tex"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/access456ory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/acce465ssory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/a456ccessory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/ac546c456essory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/ac546ces56sory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/acce546ssory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/access568ory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/acc6essory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/ac54cessory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/ac6cessory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/accesso4ry"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/accewssory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/actcessory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/access980ory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/acecessory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="@string/pay"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/accesqsory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="Text(n-1)"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

            <TextView
                android:id="@+id/accesseory"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="15dp"
                android:text="textN"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textStyle="bold|italic" />

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • code change to resolve the above issue

      <ScrollView
            android:id="@+id/body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:importantForAccessibility="yes"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView">
    

    Thanks to @Muhammad Ammar for help