Android GridLayout center horizontally
Asked Answered
E

6

20

I'm trying to create a GridLayout with 2 columns which will be centered.

My avtual design is:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    custom:rowCount="4"
    custom:columnCount="2"
    android:orientation="horizontal">
    <TimeTableKeeper.Tile
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:gravity="top|left"
        android:background="#00FF00"
        custom:color="green"
        custom:layout_row="0"
        custom:layout_column="0" />
    <TimeTableKeeper.Tile
        android:layout_width="75dp"
        android:gravity="top|left"
        android:layout_height="75dp"
        android:background="#00FF00"
        custom:color="blue"
        custom:layout_row="0"
        custom:layout_column="1" />
</GridLayout>

And it looks like:

And I would like to have this buttons in the center and perfectly with spacing between them.

Is it possible?

--EDIT:

I have also tried putting it into LinearLayout, without results:

<?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:gravity="center"
    android:orientation="vertical">
    <GridLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:rowCount="4"
        custom:columnCount="2"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_gravity="center">
        <TimeTableKeeper.Tile
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="#00FF00"
            custom:color="green"
            custom:layout_row="0"
            custom:layout_column="0" />
        <TimeTableKeeper.Tile
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="#00FF00"
            custom:color="blue"
            custom:layout_row="0"
            custom:layout_column="1" />
    </GridLayout>
</LinearLayout>
Emmaline answered 24/7, 2014 at 8:23 Comment(5)
Why don't you use the GridLayout inside a LinearLayout with the property android:gravity="center"?!Protonema
Tried, without results. Details in post editEmmaline
Of course it won't change because you have to use the property android:layout_width="wrap_content" in your GridLayout.Protonema
Thanks! What about spacing between items?Emmaline
See this: #10016843Protonema
M
44

Make the grid wrap around its content horizontally with layout_width="wrap_content" and set it's layout_gravity to center:

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    // ......
    >
Munniks answered 22/11, 2015 at 6:55 Comment(3)
No, that doesn't make it centered horizontally.Adria
This will center the cells within the grid's surface. Something like: |__CCCC__|Geulincx
this solution perfect for meSpeakeasy
D
9

From GridLayout documentation:

GridLayout's distribution of excess space is based on priority rather than weight.

(...)

To make a column stretch, make sure all of the components inside it define a gravity.

So apparently you need to set the layout_gravity to android:layout_gravity="top|center" (I have not tested this, but from the documentation it should be along these lines.)

Dempsey answered 28/7, 2014 at 15:2 Comment(1)
This should be accepted answer. As it works perfectly fineGamez
S
6

You're almost there. I think this will do the job:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    custom:rowCount="4"
    custom:columnCount="2"

    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">
    <TimeTableKeeper.Tile
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="#00FF00"
        custom:color="green"
        custom:layout_row="0"
        custom:layout_column="0" />
    <TimeTableKeeper.Tile
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="#00FF00"
        custom:color="blue"
        custom:layout_row="0"
        custom:layout_column="1" />
</GridLayout>
</FrameLayout>
Steelworker answered 24/7, 2014 at 8:44 Comment(1)
Yes, FrameLayout works much better than LinearLayout in this case.Adria
G
0

if you are using a constraint layout use wrap content on the grid layout and create the items inside it. It will be centered to the screen

<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:columnCount="2"
android:useDefaultMargins="true"
/>
Grogshop answered 1/12, 2020 at 18:14 Comment(0)
K
-1

The code in below solved my problem.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

 <GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="30dp"
    android:columnCount="3"
    android:rowCount="4"
    android:useDefaultMargins="true">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

     <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


 </GridLayout>

 </RelativeLayout>
Keratoid answered 21/10, 2019 at 14:45 Comment(0)
P
-2

copy this example for calculator layout =)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    >
    <!-- android:useDefaultMargins="true" (OPTIONAL) -->
    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:useDefaultMargins="true" 
        >
        <Button
            android:layout_column="0"
            android:layout_row="0"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="/"/>
        <Button
            android:layout_column="1"
            android:layout_row="0"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="1"/>
        <Button
            android:layout_column="2"
            android:layout_row="0"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="2"/>
        <Button
            android:layout_column="3"
            android:layout_row="0"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="3"/>
        <Button
            android:layout_column="0"
            android:layout_row="1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="*"/>
        <Button
            android:layout_column="1"
            android:layout_row="1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="4"/>
        <Button
            android:layout_column="2"
            android:layout_row="1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="5"/>
        <Button
            android:layout_column="3"
            android:layout_row="1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="6"/>
        <Button
            android:layout_column="0"
            android:layout_row="2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="-"/>
        <Button
            android:layout_column="1"
            android:layout_row="2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="7"/>
        <Button
            android:layout_column="2"
            android:layout_row="2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="8"/>
        <Button
            android:layout_column="3"
            android:layout_row="2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="9"/>
        <Button
            android:layout_column="0"
            android:layout_row="3"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="+"/>
        <Button
            android:layout_column="1"
            android:layout_row="3"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="0"/>
        <Button
            android:layout_column="2"
            android:layout_row="3"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="00"/>
        <Button
            android:layout_column="3"
            android:layout_row="3"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:text="="/>

    </GridLayout>


</LinearLayout>
Preadamite answered 7/9, 2017 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.