Search code examples
androidandroid-layoutandroid-gridlayout

How To Make Gridlayout Scrollable


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>

Solution

  • 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>