francydarkcool -
francydarkcool -

Reputation: 85

Center buttons in GridView layout

I have 4 buttons and I want to align them in center, but with some space between them. I've tried to use a Frame + GridView layout, Linear + GridView or only a GridView Layout.

After some time I've succeded to show the buttons in the center of the screen, but I cannot add any space between them. I've tried to adjust the verticalSpacing and the horizontalSpacing in the GridView layout, but with no luck. Also, I've tried to add android:height or width to the buttons and give them a value, but this didn't change anything as well....

Here is my code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_gravity="center"
    android:padding="5dp" >

    <GridLayout
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center">

            <Button
                android:id="@+id/button8"
                android:layout_width="100dp"
                android:layout_height="126dp"
                android:layout_column="0"
                android:layout_row="0"
                android:background="@drawable/toilet" />

            <Button
                android:id="@+id/button28"
                android:layout_width="100dp"
                android:layout_height="126dp"
                android:layout_column="1"
                android:layout_row="0"
                android:background="@drawable/aragaz" />

            <Button
                android:id="@+id/button56"
                android:layout_width="100dp"
                android:layout_height="126dp"
                android:layout_column="0"
                android:layout_row="1"
                android:background="@drawable/broom" />

            <Button
                android:id="@+id/button57"
                android:layout_width="100dp"
                android:layout_height="126dp"
                android:layout_column="1"
                android:layout_row="1"
                android:background="@drawable/shoe" />
        </GridLayout>
</LinearLayout>

And this is how i see my buttons:

enter image description here

Upvotes: 0

Views: 319

Answers (1)

AskNilesh
AskNilesh

Reputation: 69681

Try this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="5dp">

    <GridLayout
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="2"
        android:background="@color/red"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp">

        <Button
            android:id="@+id/button8"
            android:layout_width="100dp"
            android:layout_height="126dp"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_margin="5dp"
            android:background="@drawable/ic_launcher_background" />

        <Button
            android:id="@+id/button28"
            android:layout_width="100dp"
            android:layout_height="126dp"
            android:layout_column="1"
            android:layout_row="0"
            android:layout_margin="5dp"
            android:background="@drawable/ic_launcher_background" />

        <Button
            android:id="@+id/button56"
            android:layout_width="100dp"
            android:layout_height="126dp"
            android:layout_column="0"
            android:layout_row="1"
            android:layout_margin="5dp"
            android:background="@drawable/ic_launcher_background" />

        <Button
            android:id="@+id/button57"
            android:layout_width="100dp"
            android:layout_height="126dp"
            android:layout_column="1"
            android:layout_row="1"
            android:layout_margin="5dp"
            android:background="@drawable/ic_launcher_background" />
    </GridLayout>
</LinearLayout>

OUTPUT

![enter image description here

Upvotes: 1

Related Questions