Search code examples
androidandroid-layoutandroid-linearlayoutandroid-scrollviewandroid-relativelayout

scrollview with tablelayout and linearlayouts


I have a ScrollView layout with a TableLayout in which i have a TableRow which consists of three TextViews.

The problem arises when i try to align the TextViews with the screen. The TextViews don't take up the space of the screen and kind of cram up.

I have attached the code too if you guys need to review it ...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_reciept"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.spuck.LayoutTable">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="49dp"
            android:src="@drawable/the_one_3"
            android:id="@+id/the_one"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/scrollView2"
        android:layout_below="@+id/relativeLayout"
        android:layout_above="@+id/addButton">


            <TableLayout
                android:id="@+id/maintable"
                android:layout_width="match_parent"
                android:layout_height="300dp">

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

                    <LinearLayout android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:id="@+id/linear_layout_two_table_layout">


                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:text="Date"
                            android:textAlignment="center"
                            android:textSize="20dp"
                            android:layout_weight="0.5"
                            android:background="@drawable/cell_shape"
                            />

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:text="Session Time"
                            android:textSize="20dp"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            />

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:text="Average Speed"
                            android:layout_gravity="center"
                            android:textSize="20dp"
                            android:layout_weight="1"
                            android:background="@drawable/cell_shape"
                            />

                    </LinearLayout>
                </TableRow>
            </TableLayout>

    </ScrollView>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/addButton"
        android:text="Scan item barcode"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Solution

  • Set in LinearLayout android:layout_weight="2.5" which is sum of layout weight of LinearLayout children. After this Layout should be aligned to left and right.

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="2.5"
        android:id="@+id/linear_layout_two_table_layout">