Align Buttons in GridLayout
Asked Answered
B

2

6

My layout code and its graphical representation is:

enter image description here

This is just an example, I have about 30 Buttons in my application inside a GridLayout. I want my Buttons to fill their entire cell in the grid and the grid's columns should be in even width/height.

I cant seem to get it done, any help would be welcome.

Bellied answered 26/9, 2012 at 12:10 Comment(4)
why don't you use linear-layout, you can place all buttons in proper heigth and width.Pomeranian
This is basically a calculator application, so grid layout seems best (I've experimented with linear/relative layout but a grid suits me better)Bellied
How exactly do you want to place all the 30 Buttons? It should be a matrix of n X m Buttons, all being square(equal width and height)?Chrystalchryste
s18.postimage.org/i7rgp7tnb/Untitled_2.jpg This is my actual layout, as you can see the rightmost column is wider than the others, i need all columns to be the same width and for each button to fill it's cell entirely. the result should be that the grid layout fills the entire screen and each of its buttons has is as big as it can get.Bellied
C
9

I haven't use GridLayout to much to recommend something about using it, what I can recommend you is to use a TableLayout. I say this because your layout fits in the TableLayout's scope pretty well and after a quick browsing of GridLayout's documentation this seems like a problem:

GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space between multiple components.

Also GridLayout was introduced in ICS.

You can see an example for your layout using TableLayout here:

https://gist.github.com/3788301

If you don't want the table to fill the entire height then remove the weigthSum property from the TableLayout and layout_weight="1" from the TableRows.

Chrystalchryste answered 26/9, 2012 at 14:16 Comment(2)
@aviran If my answer helped you could accept it by clicking the check mark. This way the question will become answered , we'll get some reputation points and you're more likely to get users to help you in the future.Chrystalchryste
While GridLayout was introduced in ICS, it exists in the v7 support library as well.Standard
P
3

With API level 21+ you can use layout_rowWeight and layout_columnWeight:

 <GridLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:columnCount="2"
        android:rowCount="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:text="Button" />
        <Button
            android:id="@+id/button2"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:text="Button" />
        <Button
            android:id="@+id/button3"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:text="Button" />
        <Button
            android:id="@+id/button4"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:text="Button" />

    </GridLayout>
Piker answered 9/3, 2018 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.