0

I'm new to Android development, so forgive me if this is a noob question! I am trying to display a table containing 2 columns. The 2nd column should be right aligned, but can be a fixed width. The first column should stretch to fill any remaining space. I have a very simple table layout, but it pushes the 2nd column's text off the edge of the screen so you can't read it.

Here is the XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tbl_my_tanks"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0">

    <TableRow android:padding="3dip">
        <TextView
            android:id="@+id/col_tank_name"
            android:text="@string/tanks_tank_name" />

        <TextView
            android:id="@+id/col_stock_level"
            android:gravity="right"
            android:text="@string/tanks_stock_level" />

    </TableRow>

</TableLayout>

...and this is what it looks like when I run it:

Text disappears off right hand edge of screen in Android

How can I get it to align right correctly? (I tried using android:layout_gravity="right", but that made no difference).

Russ
  • 3,768
  • 7
  • 28
  • 37

3 Answers3

2

If you remove the padding from the table row, it should be fine. Try using margin instead.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • Thank you, removing the padding does stop it overflowing. There is no android:margin attribute though, and android:layout_margin causes the overflow problem again. So I still can't find any way of getting it to work with a bit of padding. Still, no padding is better than overflowing so I can live with it. – Russ Apr 18 '12 at 16:07
0

According to api docs a tablerow should allways be child of a tablelayout, maybe that's why it's acting funky?

Tor P
  • 1,351
  • 11
  • 9
  • There is a tablelayout - I pasted in the whole of the xml, but stackoverflow seems to have removed some of it! Or maybe I accidentally deleted it - I will try editing the question to correct. – Russ Apr 18 '12 at 15:48
  • the unit 'dip' in the padding is supposed to be 'dpi'? Probably not the cause, but if there is a typo in the xml you might want to fix it anyways... – Tor P Apr 18 '12 at 16:09
  • at the moment I can't think of any other reason for this not to work than that the parent stretches outside screen boundaries... might want to check that... – Tor P Apr 18 '12 at 16:15
  • I'm not sure what 'dip' means, but it is used extensively in the examples given in the official documentation and Eclipse nags me to use it instead of px, so I guess it must be right. I found that if I put the padding on the TextViews instead of the table rows, everything works fine. – Russ Apr 19 '12 at 12:35
0

Im having the same issue. Sometimes its fixed by compensating the clipping by adding the same dimension as right-margin to the last column of the row:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tbl_my_tanks"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0">
    <TableRow android:padding="3dip">
        <TextView
            android:id="@+id/col_tank_name"
            android:text="@string/tanks_tank_name" />
        <TextView
            android:id="@+id/col_stock_level"
            android:gravity="right"
            android:layout_marginRight="3dip"
            android:text="@string/tanks_stock_level" />

    </TableRow>
</TableLayout>

If still clipping, then just add the row-padding as margins on each column of the row.

Gero
  • 1,842
  • 25
  • 45