2

I have a GridLayout which is used to host a number of buttons, ordered in two columns. All the buttons have fixed height and width. If one of the buttons contains too much text, the layout is messed up. I'd like for the layout to maintain the rows correctly, regardless of whether buttons have too much text or not (I'll handle the case of displaying too much text later, using auto size of text).

This is my code:

<GridLayout
        android:id="@+id/cont_middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="2dp"
        android:columnCount="2"
        >

        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="this is a really long text"/>
        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
        <Button android:layout_width="120dp" android:layout_height="60dp" android:text="test"/>
</GridLayout>

This is how it looks:

enter image description here

this is how the layout should look (nevermind the text)

enter image description here

Note: I don't want to force a single line, I'd like for text to wrap if it wants, the button does not appear to change it's height, but I don't want to have it move around.

What am I missing?

Thank you.

SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65
  • 1
    please use the marquee tag for button view.. may it will help you.:) – V.P. May 27 '14 at 13:23
  • `android:layout_width="0dp"` worked for me to get text wrap to work in a grid layout that has all buttons - thanks to this post: https://stackoverflow.com/a/23090059/2162226 – Gene Bo Oct 26 '17 at 21:42

2 Answers2

4

You should place the button inside a linearLayout and set the sizes of your button to the linear layout. Inside the linear layout place your button as both width and height as fill parent. This way your view will not be messed up.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • This worked like a charm, thank you, do you know why this happened? – SoManyGoblins May 27 '14 at 13:29
  • lol coz i am awsome your button is now bound to the boundary of its parent linear layout just like any layout is bound to the physical dimension of the screen mind accepting my question then any ways glad to help – Illegal Argument May 27 '14 at 13:31
  • 1
    I understand that, but what I was interested in knowing was why it required a wrapper to behave properly. But, I guess it's just an Android bug. Thanks a bunch, I was waiting for SO to allow me to accept your answer, you reponded too quickly. Thank you! – SoManyGoblins May 27 '14 at 13:58
2

you probably can do it by simply adding a maximum height to the buttons

android:maxHeight="60dp"
Budius
  • 39,391
  • 16
  • 102
  • 144