-2

I'm having a hard time getting wrap_content to work with TableLayout. Here is the XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">   
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:stretchColumns="*"              
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >    
        <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >    
                <TextView
                android:id="@+id/textView1"
                android:text="Column 1" />
                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
        </TableRow>
    </TableLayout>
    <Button  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:layout_weight="1"           
        android:text="Button 2" />                          
</LinearLayout> 

And this is what it looks like:

Screenshot

As you can see, although layout_weight of the TableLayout and the Button are both 1, the button is crushed to the right.

As discussed here, I know I can solve this by setting the layout_width of the TableLayout to 0dp but I don't want to do that. I specifically want to use wrap_content as the layout_width for the TableLayout because using 0dp instead of wrap_content has an implication that I don't want.

Namely, when using 0dp instead of wrap_content, LinearLayout will assume a width of 0 for the TableLayout and then distribute the remaining space among all children. When using wrap_content, however, LinearLayout will first compute the minimum width of the child, subtract it from the remaining space, and then distribute the remaining space among the children, which will lead to quite different results which are important for my specific use case.

That's why I need to find a way to use wrap_content with TableLayout. When using a LinearLayout instead of a TableLayout, setting layout_width to wrap_content works fine. It's only TableLayout that doesn't seem to be compatible with wrap_content. Is this an Android bug or am I doing something wrong here?

Andreas
  • 9,245
  • 9
  • 49
  • 97
  • what exactly do you want i din't get it can you pain the view. – Shubham Vala Jul 31 '18 at 12:23
  • I want to use `wrap_content` for `layout_width` of the `TableLayout`, together with `layout_weight` of 1. The issue is in the distribution of space inside the `LinearLayout`. Assume the `LinearLayout` width is 1000 units and there are two children with `layout_width` set to 0 and `layout_weight` set to 1. Each will get 500 units. Now if the `layout_width` of both children is set to `wrap_content` and `layout_weight` to 1, Android will first subtract the minimum width of each child from the 1000 units and then distribute the remaining space... – Andreas Jul 31 '18 at 12:34
  • ...for example if the width of `TableLayout` is 200 and the width of the `Button` is 100, then only 700 units will be left to distribute in the `LinearLayout`. The `TableLayout` will get an additional 350 and the `Button` too so the final width will be 550 for the `TableLayout` and 450 for the `Button`. That's the difference between setting `layout_width` to `wrap_content` or to 0. – Andreas Jul 31 '18 at 12:34
  • ok i get it your point – Shubham Vala Jul 31 '18 at 12:42

1 Answers1

0

make android:layout_width="0dp"`

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:stretchColumns="*"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView1"
            android:text="Column 1" />
        <Button
            android:id="@+id/button1"
            android:text="Column 2" />
    </TableRow>
</TableLayout>
<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button 2" />

Vinay
  • 732
  • 5
  • 8
  • Thanks, but please read my post again. I specifically *don't* want to set `layout_width` to 0dp. – Andreas Jul 31 '18 at 13:15
  • try to make Button android:layout_width="match_parent" and TableLayout android:layout_width="wrap_content" – Vinay Jul 31 '18 at 13:18
  • I also want the button to use `wrap_content`. – Andreas Jul 31 '18 at 13:24
  • that means you want to Button and TableLayout always android:layout_width="wrap_content" but distributed in same ratio like using android:layout_width="0dp"? – Vinay Jul 31 '18 at 13:30
  • See my comments below my original post. I think this explains it really well. I don't want the distribution like with 0dp but like with wrap_content. – Andreas Jul 31 '18 at 13:38