3

I have TableLayout and I would like to have buttons as cells. All buttons should have the same width. And all buttons should have the same height. (Weight and height will be different as I want to fill the screen).

I was sure that I'm doing everything right but unfortunately only the width is the same. The problem is with height - first two rows have the same height, the third is smaller (or more precisely as long as buttons doesn't contain any text, the height is the same, but when I put text, the height is not equal). I couldn't believe that it doesn't work so I made even screenshoot and I checked the distance in graphical program.

My table:

<TableLayout
    android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:stretchColumns="*"
android:shrinkColumns="*"
android:weightSum="3"
android:padding="@dimen/dialog_margin"
>

    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stretchColumns="*"
        android:gravity="center"
        android:weightSum="2">

        <Button
            android:id="@+id/button_0"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <Button
            android:id="@+id/button_1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />

    </TableRow>

    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stretchColumns="*"
        android:gravity="center"
        android:weightSum="2">

        // buttons analogously as in previous row....
    </TableRow>

    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stretchColumns="*"
        android:gravity="center"
        android:weightSum="2">

        // buttons analogously as in previous row....
    </TableRow>

I tried also to set the TableRow height as match_parent. It doesn't help.

And btw how shrinkColums work, in particular together with stretchColumns? (I had version with and without shrinkColumns and it seems there is no difference.

user2707175
  • 1,133
  • 2
  • 17
  • 44

2 Answers2

0

I think, it's not really possible not to change width when a text is added. But I think you can try this. When you will add the text write it like this:

setText("<html>your text<html>");

it might help, but maybe the text won't be there in full size

martin k.
  • 156
  • 3
  • 15
0

Use this for the tableLayout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

and this for each row

   <TableRow 
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center">
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • 1
    setting layout_height for TableLayout to match_parent doesn't change anything. Even eclipse suggest to put 0dip here. But in both cases it doesn't work (last row height is smaller). When it comes TableRow attributes it does neither help. – user2707175 Feb 15 '14 at 18:47
  • Strange... I copied it from an xml in Eclipse, and I'm getting exactly what you're looking for. – Merlevede Feb 15 '14 at 18:51
  • Strange but in Eclipse all fields have the same height in my case too. But when I run it on device with android 4.0.4 they don't. Moreover as I add the text dynamically when creating view, in Eclipse I don't have any text in Buttons. Then they have the equal height. Moreover it's also quite suprising as text is not terribly long (even if each button has different text with slightly different lenght) and even if first two rows were smaller, the text would also fit them. THus I really don't understand why it doesn't work. – user2707175 Feb 15 '14 at 18:54
  • I also disovered when the text is the same in all cells, then the height is also the same. I'm wondering if it has any relation with new line character I use to break the text (\n). As some cells doesn't have this new line while some have one or two.... – user2707175 Feb 15 '14 at 18:58
  • Even if I remove new line characters, the heigh is different. It stays the same only when all buttons have the same text. – user2707175 Feb 15 '14 at 19:40