grooble
grooble

Reputation: 657

Android: How to balance GridLayout in xml

I want to have a layout with a header and a footer and four pictures (ImageView) all the same size in two rows and two columns in the center. I don't want to use GridView.

This is what I'm thinking of:

Header and footer with block of four ImageViews in center (they are currently buttons)

the GridLayout elements go their own way with size and don't shrink to the size imposed by the surrounding elements. They either become too big and go off the page, or are cropped by the following LinearLayout. Here is an example of what I mean:

Screenshot on Nexus 5

I guess you'll need to see the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

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

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_gravity="fill_vertical|fill_horizontal"
            android:layout_row="0"
            android:layout_weight="1"
            android:columnCount="2"
            android:weightSum="4"
            android:rowCount="2" >

    <Button
        android:id="@+id/button2"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button13"
        android:layout_column="1"
        android:layout_row="0"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button14"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button15"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

        </GridLayout>
    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

I want:

Maybe I'm taking the wrong approach to this, so feel free to send me off in a new direction if necessary.

Upvotes: 1

Views: 930

Answers (1)

Pratik Butani
Pratik Butani

Reputation: 62421

Without GridView you can try it:

It works perfect.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_row="0"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button13"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_row="0"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button14"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button15"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions