kittu88
kittu88

Reputation: 2461

Table layout not fitting screen

I have an activity which creates a table dynamically. The activity is created and is working fine.

This is the source code of the xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <HorizontalScrollView
                        android:id="@+id/horizontalScrollView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" >

                        <TableLayout
                            android:id="@+id/follow_up_table"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent" 
                            android:stretchColumns="0">


                        </TableLayout>

                    </HorizontalScrollView>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

The output of the xml file:

output

I want the table to fill the whole box, the space on the right hand side of the table is not required and is looking awkward. What is to be done to achieve this?

Upvotes: 3

Views: 6137

Answers (5)

Jordan Hochstetler
Jordan Hochstetler

Reputation: 1416

Better yet stretch and shrink all columns to fit your screen with:

<TableLayout
 android:id="@+id/follow_up_table"
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:stretchColumns="*"
 android:shrinkColumns="*">

</TableLayout>

Upvotes: 3

kittu88
kittu88

Reputation: 2461

The layout was required to be edited like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TableLayout
                        android:id="@+id/follow_up_table"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" 
                        android:stretchColumns="0,1">


                    </TableLayout>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

The output:

Output

Upvotes: 1

Dmitry Guselnikov
Dmitry Guselnikov

Reputation: 1046

What is the purpose of using HorizontalScrollView? Just remove it and set TableLayout's android:layout_width to match_parent.

Upvotes: 1

Manoj Fegde
Manoj Fegde

Reputation: 4771

Do the change as :

<TableLayout
    android:id="@+id/follow_up_table"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="0">
</TableLayout>

Change is :

android:layout_width="**fill_parent**"

Upvotes: 1

bugraoral
bugraoral

Reputation: 2670

Your table layout width wraps its content, it should be match_parent

<TableLayout
 android:id="@+id/follow_up_table"
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:stretchColumns="0">


</TableLayout>

Upvotes: 1

Related Questions