Search code examples
androidxmlandroid-layoutandroid-constraintlayoutgrid-layout

Added ScrollView In Constaint Layout With GridLayout Its Not Working


I am trying to make a dashboard that looks like this on this link, but with one column

I want to make the dashboard to be scrollable.

I want to add a ScrollView on the below code.I have tried every means but it keeps distorting the GridLayout.

I have tried adding the ScrollView above GriLayout. It completely distorts the GridLayout.

Here is the code

<?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="match_parent"
    android:orientation="vertical"
    tools:context=".activity.Dashboard">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        style="@style/HeaderBar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/AppTheme"
        app:theme="@style/AppTheme"
        tools:targetApi="lollipop" />

    <View
        android:id="@+id/bg_top_header"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/ic_bg_topheader"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:alignmentMode="alignMargins"
        android:columnCount="1"
        android:columnOrderPreserved="false"
        android:nestedScrollingEnabled="true"
        android:padding="10dp"
        android:rowCount="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="@id/bg_top_header"
        tools:targetApi="lollipop">


        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:background="@color/whiteColor"
            app:cardCornerRadius="8dp"
            app:cardElevation="6dp"
            tools:targetApi="lollipop">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:background="@mipmap/rgil_glasses"
                    android:gravity="start" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="HouseHold Register"
                        android:textColor="@color/colorBlack"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="MOH 513"
                        android:textColor="@color/colorBlack"
                        android:textSize="30sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:background="@color/whiteColor"
            app:cardCornerRadius="8dp"
            app:cardElevation="6dp"
            tools:targetApi="lollipop">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:background="@mipmap/rgil_glasses"
                    android:gravity="start" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="Service Delivery Logbook"
                        android:textColor="@color/colorBlack"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="MOH 514"
                        android:textColor="@color/colorBlack"
                        android:textSize="30sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:background="@color/whiteColor"
            app:cardCornerRadius="8dp"
            app:cardElevation="6dp"
            tools:targetApi="lollipop">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:background="@mipmap/rgil_glasses"
                    android:gravity="start" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="Diarrhoea Baseline"
                        android:textColor="@color/colorBlack"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:fontFamily="@font/mo_re"
                        android:text="Survey"
                        android:textColor="@color/colorBlack"
                        android:textSize="30sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <!----Other Views----->


    </GridLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here


Solution

  • You should add ScrollView as a root of the Layout and add constraint layout as its child.

    I had edited your code. please try there are some changes I did. it is working.

    <?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="match_parent"
        android:fillViewport="true"
        tools:context=".activity.Dashboard"
        >
    
    <androidx.appcompat.widget.Toolbar
            android:id="@+id/my_toolbar"
            style="@style/HeaderBar"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:elevation="4dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/AppTheme"
            app:theme="@style/AppTheme"
            tools:targetApi="lollipop"
            />
    
    <View
            android:id="@+id/bg_top_header"
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:layout_marginTop="50dp"
            android:background="@drawable/ic_bg_topheader"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    
    
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
            >
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
    
    
            <GridLayout
                    android:id="@+id/gridLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:alignmentMode="alignMargins"
                    android:columnCount="1"
                    android:columnOrderPreserved="false"
                    android:nestedScrollingEnabled="true"
                    android:padding="10dp"
                    android:rowCount="5"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@id/bg_top_header"
                    tools:targetApi="lollipop"
                    >
    
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="HouseHold Register"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="MOH 513"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Service Delivery Logbook"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="MOH 514"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Diarrhoea Baseline"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Survey"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Diarrhoea Baseline"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Survey"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Diarrhoea Baseline"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Survey"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
                <androidx.cardview.widget.CardView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_rowWeight="1"
                        android:layout_columnWeight="1"
                        android:layout_marginBottom="16dp"
                        android:background="@color/whiteColor"
                        app:cardCornerRadius="8dp"
                        app:cardElevation="6dp"
                        tools:targetApi="lollipop"
                        >
    
                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:orientation="horizontal"
                            >
    
                        <ImageView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="start"
                                android:background="@mipmap/rgil_glasses"
                                android:gravity="start"
                                />
    
                        <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                >
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Diarrhoea Baseline"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="20sp"
                                    android:textStyle="bold"
                                    />
    
                            <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:layout_marginTop="8dp"
                                    android:fontFamily="@font/mo_re"
                                    android:text="Survey"
                                    android:textColor="@color/colorBlack"
                                    android:textSize="30sp"
                                    android:textStyle="bold"
                                    />
                        </LinearLayout>
                    </LinearLayout>
    
                </androidx.cardview.widget.CardView>
    
    
            </GridLayout>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    </ScrollView>