How To Make Gridlayout Scrollable

Viewed 5180

I Have created a GridLayout with CardView inside a Viewpager. My layout was linear layout and to make it scrollable I wrapped whole in a ScrollView. But I can't scroll my screen. When I try to scroll nothing happens. How I can make these grids items scrollable. I am a beginner at android.

My Gridlayout 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:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginTop="150dp"
        app:columnCount="2"
        app:rowCount="4">


        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginLeft="10dp"
            app:cardCornerRadius="6dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginLeft="18dp"
            android:layout_marginStart="18dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />

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

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_marginTop="20dp"
            android:layout_height="170dp"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_height="170dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />

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

        <android.support.v7.widget.CardView
            android:layout_width="160dp"
            android:layout_marginTop="20dp"
            android:layout_height="170dp"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp">

            <ImageView
                android:layout_width="160dp"
                android:layout_height="130dp"
                android:src="@drawable/kerala" />
        </android.support.v7.widget.CardView>

    </android.support.v7.widget.GridLayout>
    </LinearLayout>
</ScrollView>
4 Answers

Try this out:

<?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:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_marginTop="150dp"
    app:columnCount="2"
    app:rowCount="4">


    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginLeft="10dp"
        app:cardCornerRadius="6dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />

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

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_marginTop="20dp"
        android:layout_height="170dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_height="170dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />

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

    <android.support.v7.widget.CardView
        android:layout_width="160dp"
        android:layout_marginTop="20dp"
        android:layout_height="170dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp">

        <ImageView
            android:layout_width="160dp"
            android:layout_height="130dp"
            android:src="@drawable/kerala" />
    </android.support.v7.widget.CardView>

</android.support.v7.widget.GridLayout>
</LinearLayout>

<android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        app:columnCount="2"
        app:rowCount="4">

Remove static height of the GridLayout then it will works fine.

try this : main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

page.xml

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:rowCount="4">

            <!-- fill your grid -->

        </GridLayout>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
  1. Create a Scroll View
  2. Inside that create your Grid Layout.
  3. When you add item to grid layout, depend on Column count Grid layout will start to scroll.

Example:

<ScrollView

    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"
    tools:context="com.groot.rang.MainActivity">

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

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight = "1"
        android:columnCount="3"
        android:rowCount="5"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:padding="10dp">

        //Your grid items..

    </GridLayout>

    </LinearLayout>
</ScrollView>
Related