0

I have a table layout inside a linear layout after inflating 3 buttons in the table row the last button go off screen I mean the 3 buttons don't fit the screen width what should i do?

here is the code

<TableLayout
    android:id="@+id/buttonTableLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:stretchColumns="0,1,2"
    android:layout_weight="1">

    <TableRow
        android:id="@+id/tableRow0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TableRow>

</TableLayout>
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Nima
  • 41
  • 1
  • 2
  • 7

1 Answers1

1

The TableRow doesn't check if it's content fit the Screen Size. I guess your Buttons take all of the Space and therefore the last Buttons is outside of the Screen.

I can think of 2 Solutions for this issue:

  • Use a LinearLayout with weights for each Button:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_weight="1.0" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:layout_weight="1.0" />
    
    </LinearLayout>
    

    You can do this programmatically too of course. More about weights can be found here: https://developer.android.com/guide/topics/ui/layout/linear.html

  • Use a Flow-Layout. It automatically checks if your Views fit in the current line and places them in the next if there is not enough space. A good Open Source Library I use myself is this one: https://github.com/ApmeM/android-flowlayout

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
rubengees
  • 1,841
  • 2
  • 16
  • 31
  • The think is that I'm using Layout Inflator service and I can't add the buttons like you said and the text of the buttons are diffrent (in lenght ) everytime that I launch the app(i pick some random text from a string array) - like a quiz – Nima Jul 12 '15 at 22:14
  • As I said you can also do this programmatically. See this Question: http://stackoverflow.com/questions/3224193/set-the-layout-weight-of-a-textview-programmatically – rubengees Jul 13 '15 at 11:09