0

I'm trying to implement a 40% – 20% – 20% – 20% split (or something close to that) with four TextViews inside a TableLayout.

Problem is, the leftmost column takes too much space. In other words, the 3 columns on the right do not take the space they need. The longer text on the left should be split on multiple lines if needed, never the 3 shorter texts on the right.

enter image description here
The text marked with red should not be split on two lines.

I even tried android:maxLines="1" on the right-hand columns, and android:lines="2" on the leftmost column, and still it won't work as intented (→ there's just one line alright, but rightmost columns get clipped).

I also tried this suggestion and set android:layout_width="0dp" on the leftmost column. Well, that gives me the opposite problem: that column takes too little space (long titles get clipped), and the 3 on the right take too much.

enter image description here
Opposite problem with android:layout_width="0dp"

I don't want to hardcode any DP width values for the columns, to best support different screens. To me, specifying relative widths with layout_weight sounds exactly what I need, if only I got it working... Any ideas what I'm doing wrong?

Android 4.4.2. My test device is Nexus 7, on which I run into this problem in portrait mode.

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />

Layout XML for one row (corresponding to the first screenshot above):

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

    <TextView
        android:id="@+id/title"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="center_vertical|left"
        android:textSize="16dp"
        android:textStyle="bold"

        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        />

    <TextView
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:text="(todo)"
        android:textSize="16dp"
        android:layout_marginTop="8dp"
        />

    <TextView
        android:id="@+id/pages_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textSize="16dp"
        android:maxLines="1"

        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        />

    <TextView
        android:id="@+id/date_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center_vertical|right"
        android:textSize="16dp"

        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        />

</TableRow>

Surrounding TableLayout and ScrollView:

 <ScrollView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <TableLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">

         <!-- table rows added dynamically in code -->

     </TableLayout>    
 </ScrollView>
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372

2 Answers2

0

Well, as is typical, I found the answer myself soon after asking here.

What I needed was:

android:shrinkColumns="0"

on the TableLayout. The shrinkColumns attribute tells the layout which columns are eligible for shrinking. (Zero-based index, so 0 means the leftmost column; you can specify several columns with comma as separator: 1, 2, 5.)

I found the answer here (but I'm not sure if this qualifies as exact duplicate).

Edit: I ended up throwing away all the layout_weight attributes from the TextViews as they didn't seem to have any effect. Also layout_width="wrap_content" on the columns turned out unnecessary. Just shrinkColumns="0" on the layout does what I want.

Edit 2: Added back the layout_weight's to each column (0.4, 0.2, 0.2, 0.2) because otherwise in landscape, with plenty of room, the rows wouldn't use all the available horizontal space.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
0

Use this xml code:

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

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="center_vertical|left"
    android:textSize="16dp"
    android:textStyle="bold"

    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="10dp"
    />

<TextView
    android:id="@+id/rating"
      android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center"
    android:text="(todo)"
    android:textSize="16dp"
    android:layout_marginTop="8dp"
    />

<TextView
    android:id="@+id/pages_read"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center"
    android:textSize="16dp"
    android:maxLines="1"

    android:layout_marginTop="8dp"
    android:layout_marginLeft="5dp"
    />

<TextView
    android:id="@+id/date_time"
       android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center_vertical|right"
    android:textSize="16dp"

    android:layout_marginTop="8dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    />

   </TableRow>
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47