Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableLayout with layout_width=matchparent not matching parent

I have a tableLayout with two columns and two rows, both rows and the last column have match_parent for width but the layout isn't filling the parent width, it comports itself like it had wrap_content.

Here is the code:

<TableLayout android:layout_width="match_parent">
    <TableRow android:layout_width="match_parent">
        <TextView 
            android:layout_width="wrap_content"
            android:text="static" />
        <EditText
            android:layout_width="match_parent"
            android:text="text" />
    </TableRow>

    <TableRow android:layout_width="match_parent">
        <TextView 
            android:layout_width="wrap_content"
            android:text="static2" />
        <EditText
            android:layout_width="match_parent"
            android:text="text2" />
    </TableRow>
</TableLayout>

What I need to do for each line having the width of the parent?

Ps: the place where I work don't allow me to post my code, so I write a code as close as possible of my code. I don't know if it is right, I can't test.

like image 202
jonathanrz Avatar asked May 23 '13 12:05

jonathanrz


2 Answers

Try this code, I think it will help you:

  <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    >

    <TableRow android:layout_width="match_parent" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:text="static" />

        <EditText
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="text" />
    </TableRow>

    <TableRow android:layout_width="match_parent" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:text="static2" />

        <EditText
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="text2" />
    </TableRow>
</TableLayout> 
like image 200
android_dev Avatar answered Sep 27 '22 21:09

android_dev


Also consider if there are other rows whose content is wider than the other rows containing a TextView, since the weight attribute won't work well. Consider this example:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Short Text" />

              <EditText
                  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" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Short Text"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </TableRow>

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

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reaaaaaaaaaaaaaaaaaaaaaaaaaally lengthy and text"/>

    </TableRow>

So this is the result:

  • The third row cell is setting every left cell width.
  • The EditText in the first row starts where the left cell ends (almost at three quarters of the screen width).
  • The second row is given weight 1 each cell, so both cells in the row should measure half the screen, but since the first cell is that wide, the weight is calculated in proportion, so even if you set a weight of 50 for the right cell, you'll never get half the screen because the first cell cannot be smaller than the content on the third cell.
like image 30
Juan José Melero Gómez Avatar answered Sep 27 '22 22:09

Juan José Melero Gómez