Android, Horizontal and Vertical Scroll for GridLayout

Nivaldo Bondança picture Nivaldo Bondança · Mar 1, 2013 · Viewed 18.4k times · Source

I'm having trouble getting a GridLayout to scroll horizontally.

I found a similar question Gridlayout + ScrollView. I tried that method, but it didn't work.

It cuts out many tables (because it was supposed to go display all tables from 1 to 20).

Here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="16dp" >

            <android.support.v7.widget.GridLayout
                android:id="@+id/table_mapGrid"
                android:layout_width="250dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

    <include layout="@layout/cell_list_loading" />

    <TextView
        android:id="@+id/table_errorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:text="@string/message_error_connection"
        android:visibility="invisible" />

</FrameLayout>

I want to have dynamic content displayed, varying the number of columns and rows possibly with empty spaces between tables. This I have accomplished, but the problem is when the width of the GridLayout becomes greater than its container's, I wanted to solve that using horizontal scroll, but it doesn't seem to work...

Any suggestion?

Answer

Nivaldo Bondan&#231;a picture Nivaldo Bondança · Mar 9, 2013

Well I found the solution

It seems like the android ScrollView works as a VerticalScrollView and only that (the name is not so intuitive as HorizontalScrollView).

So to make something scrollable vertically and horizontally, you need to nest a (Vertical)ScrollView inside a HorizontalScrollView, or the other way around, like this

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

            <!-- Your content here -->

     </HorizontalScrollView>
</ScrollView>