Greg
Greg

Reputation: 1803

Android Studio - table layout sizes?

New to Android Studio/java programming. I need to create a table with 3 cells that are the same size.

I've got something more or less working but am having problems with the sizing of the tables. How can I get the size of the cells all the same width. Ie each one a 1/3 of the screen?

<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/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin"
 tools:context="com.example.List">

 <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TableRow android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:text="long string 1"
        android:textColor="#ffffff"
        android:background="#000000"
        android:layout_marginStart="5dp"
        android:layout_height="wrap_content">
    </TextView>

    <TextView
        android:text="string 2"
        android:textColor="#000000"
        android:gravity="center_horizontal"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:background="#ffffff"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:layout_width="wrap_content">
    </TextView>

        <TextView
            android:text="s3"
            android:textColor="#000000"
            android:gravity="center_horizontal"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:background="#ffffff"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:layout_width="wrap_content">
        </TextView>
    </TableRow>

 </TableLayout>


</RelativeLayout>

enter image description here

Upvotes: 1

Views: 4579

Answers (3)

M S Gadag
M S Gadag

Reputation: 2057

try this..

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.List" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:weightSum="9" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_weight="3"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:paddingBottom="16dp"
                android:paddingTop="16dp"
                android:text="long string 1"
                android:textColor="#ffffff" >
            </TextView>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="3"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:paddingBottom="16dp"
                android:paddingTop="16dp"
                android:text="string 2"
                android:textColor="#000000" >
            </TextView>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="3"
                android:background="#000000"
                android:gravity="center_horizontal"
                android:paddingBottom="16dp"
                android:paddingTop="16dp"
                android:text="s3"
                android:textColor="#000000" >
            </TextView>
        </TableRow>
    </TableLayout>

</RelativeLayout>

Upvotes: 1

hoi
hoi

Reputation: 2188

Change layout_width of the three TextView from "wrap_content" to "0dp" because of parent's(TableRow) orientation is horizontal.

Upvotes: 0

migue02
migue02

Reputation: 130

Try changing the layout_width of the three TextView from wrap_content to match_parent, it will work because of their weight is set to '1'

Upvotes: 0

Related Questions