irobotxx
irobotxx

Reputation: 6063

TableRow taking more space than usual android TableLayout

Good day, i am having troubles with the following image below. As you can see, the table rows are taking so much space and i would like to reduce them because there are other view widgets under them. how can i achieve this? Thank you.

enter image description here

this is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TableLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="3"
    android:padding="8dp"
    android:background="@drawable/rounded_date_filter">

  <TableRow
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1">

 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/from_text">
</TextView>
<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="01-01-2012">
</TextView>
</TableRow>

  <View
      android:layout_width="fill_parent"
      android:layout_height="1px"
      android:background="#606060">
  </View>

<TableRow>
 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/to_text">
</TextView>

<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="01-09-2012">
</TextView>
</TableRow>

      <View
      android:layout_width="fill_parent"
      android:layout_height="1px"
      android:background="#606060">
  </View>

    <TableRow>
 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/groupby">
</TextView>

<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="day">
</TextView>
</TableRow>

</TableLayout>

    <Button
        android:id="@+id/reset_filter_button_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/reset_filters">
    </Button>
</LinearLayout>

Upvotes: 2

Views: 1754

Answers (3)

sunil
sunil

Reputation: 6614

Here is the corrected layout file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:weightSum="3" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="From" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="01-01-2012" >
            </TextView>
        </TableRow>

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:layout_weight="1"
                android:background="#606060" >
            </View>
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="To" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="01-09-2012" >
            </TextView>
        </TableRow>

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:layout_weight="1"
                android:background="#606060" >
            </View>
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Group By" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="day" >
            </TextView>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/reset_filter_button_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My Button" >
    </Button>

</LinearLayout>

and the output layout :

enter image description here

The problems i noted and hence corrected are:

  • Added the the row separation as a separate row, instead of ambiguous view inside TableLayout
  • Changed the layout_width of TableLayout from wrap_content to fill_parent

Upvotes: 0

user
user

Reputation: 87064

I've tested your layout and in the end it appears the weightSum and layout_weight are not responsible. Setting the TableLayout to wrap the content and the layout_weight on the TextViews seems to expand the TableLayout and make it have the behavior you see.

If you set the width of the TableLayout to FILL_PARENT the problems should go away:

// ...
<TableLayout 
    android:layout_width="fill_parent"
// ...

This somehow makes sense as you try to use the layout weight when the parent doesn't really have space to spare(because of the wrap_content), nothing good can come from this.

Also, if that is all your layout, you could improve it and add the Button directly to the TableLayout(in another TableRow) removing the need for the wrapper LinearLayout.

Upvotes: 1

knvarma
knvarma

Reputation: 974

The layout_weight is used to tell the layout to forcefully fill the space. In above layout the TableRow weight is specified as 1 and weighSum as 3. Remove the layout_weight and weightSum it will work.

Upvotes: 0

Related Questions