How to adjust width of the table layout columns in android

200 views Asked by At

Here i am developing an application that consists of the table layout and I am displaying in 9.7 inches display but here by using table layout it was overflowing out of the screen and i need to decrease the width of the column of table row. Can any one tell me how to adjust the width of the table row in android

This is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/default_app_widget_padding_left"
    android:paddingRight="@dimen/default_app_widget_padding_right"
    tools:context="com.developer.milanandroid.Piston" >

<RelativeLayout
        android:id="@+id/relative_layout_Piston"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:elevation="5dp"
         >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_below="@+id/pager_sliding_tab_strip"
        android:weightSum="4" 
        android:background="#FFFFFF">

       <TextView
            android:id="@+id/txt_s.no"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            android:layout_marginLeft="8dp"
            android:text="@string/sno"
            android:gravity="center"
            android:textSize="15dp" />

    <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="#9e9e9e" 
        android:layout_marginLeft="50dp"/>

        <TextView
            android:id="@+id/txt_test_point_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="59dp"
            android:text="@string/Description"
            android:textSize="15dp" 
            android:gravity="center"/>

        <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="#9e9e9e" 
        android:layout_marginLeft="100dp"/>


        <TextView
            android:id="@+id/txt_Description"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="73dp"
            android:gravity="center"
            android:text="@string/TestPointname"
            android:textSize="15dp" />

        <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="#9e9e9e" 
        android:layout_marginLeft="41dp"/>


        <TextView
            android:id="@+id/txt_Range"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="57dp"
            android:text="@string/Range"
            android:textSize="15dp" 
            android:gravity="center"/>

        <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="#9e9e9e" 
        android:layout_marginLeft="102dp"/>


        <TextView
            android:id="@+id/txt_Measurement"
            android:layout_width="82dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:text="@string/Measurement"
            android:textSize="15dp" 
            android:gravity="center"/>
    </LinearLayout>
    <View android:id="@+id/view_1"
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="#BDBDBD"
          android:layout_below="@+id/linearLayout1"/>
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/relative_layout_Piston" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:stretchColumns="4"
        android:shrinkColumns="3" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center"
            >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="50dp"
                android:background="@drawable/cell_shape"
                android:paddingRight="50dp"
                android:text="@string/TP_27_Sno"
                android:textSize="15dp"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/TP_27_Description"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:textSize="15dp"
                android:layout_weight="1"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Test_Point_27"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/TP_27_Range"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\n\n\n\n"
                android:textSize="15dp"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center"

            >

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_28_Sno"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_28_Description"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:gravity="center"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Test_Point_28"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_28_Range"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\n\n\n"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center">
             <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_29_Sno"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_29_Description"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:gravity="center"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Test_Point_29"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_29_Range"
                android:paddingLeft="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:paddingRight="50dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\n\n"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:gravity="center"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_30_Sno"
                android:gravity="center"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_30_Description"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Test_Point_30"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TP_30_Range"
                android:paddingLeft="50dp"
                android:textSize="15dp"
                android:gravity="center"
                android:paddingRight="50dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\n\n"
                android:gravity="center"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:textSize="15dp"
                android:background="@drawable/cell_shape"
                android:textAppearance="?android:attr/textAppearanceMedium" />
         </TableRow>



         </TableLayout>
</ScrollView>

</RelativeLayout>

This is my drawable file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"
    >
    <solid android:color="#FFFFFF"/>
    <stroke android:width="1dp" android:color="#9e9e9e"/>


</shape>
1

There are 1 answers

0
Zurvæ On

Just specify the layout_width and layout_height properties on the parent view for the row as a specific height such as 100dp. If you need to change the value based on different screen sizes use the dimens.xml file to specify different values for each screen type you want to.