Ramya Selvarani Ramya Selvarani - 2 months ago 6
Android Question

how to give first column as empty and second column contains button in TableLayout in android

I am designing a login page which has

EditText
and
TextView
for username & password and buttons for sign-in & reset. I need to place my buttons in a single column so that the first column should be empty and second column should have 2 buttons. Can anyone help me.

Thanks in advance.

This is my xml code.

<TableLayout
android:id="@+id/con1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25pt"
android:layout_marginRight="25pt"
android:gravity="center"
android:orientation="vertical" >
<TableRow android:layout_width="match_parent" android:gravity="center">

<TextView
android:id="@+id/lbl_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="76px"
android:layout_y="24px"
android:text="SignIn Here"
android:gravity="center"
>
</TextView>
</TableRow>

<TableRow android:layout_width="fill_parent">


<TextView
android:id="@+id/lbl_username"
android:layout_width="20pt"
android:layout_height="wrap_content"
android:layout_x="19px"
android:layout_y="57px"
android:gravity="left"
android:text="Username"
android:layout_weight="0.5">

</TextView>

<EditText
android:id="@+id/txt_username"
android:layout_width="20pt"
android:layout_height="wrap_content"
android:layout_x="54px"
android:layout_y="80px"
android:textSize="18sp"
android:layout_weight="0.5">
</EditText>
</TableRow>

<TableRow>

<TextView
android:id="@+id/lbl_password"
android:layout_width="20pt"
android:layout_height="wrap_content"
android:layout_x="21px"
android:layout_y="131px"
android:text="Password"
android:layout_weight="0.5">
</TextView>

<EditText
android:id="@+id/txt_password"
android:layout_width="20pt"
android:layout_height="47px"
android:layout_x="53px"
android:layout_y="155px"
android:password="true"
android:textSize="18sp"
android:layout_weight="0.5">
</EditText>
</TableRow>

<TableRow>

<LinearLayout >
<Button
android:id="@+id/btn_signin"
android:layout_width="80pt"
android:layout_height="wrap_content"
android:layout_x="52px"
android:layout_y="200px"
android:text="SignIn"
android:layout_weight="0.025">
</Button>

<Button
android:id="@+id/btn_reset"
android:layout_width="80pt"
android:layout_height="wrap_content"
android:layout_x="123dp"
android:layout_y="201px"
android:text="Reset"
android:layout_weight="0.025">
</Button>
</LinearLayout>
</TableRow>

</TableLayout>

Answer

try this:

 <TableLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/con1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25pt"
    android:layout_marginRight="25pt"
    android:gravity="center"
    android:orientation="vertical" >

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

    <TextView
        android:id="@+id/lbl_heading"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_x="76px"
        android:layout_y="24px"
        android:text="SignIn Here"
        android:gravity="center" 
      >
    </TextView>
    </TableRow>

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

    <TextView
        android:id="@+id/lbl_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_x="19px"
        android:layout_y="57px"
        android:gravity="left"
        android:text="Username" >

    </TextView>

    <EditText
        android:id="@+id/txt_username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_x="54px"
        android:layout_y="80px"
        android:textSize="18sp" >

    </EditText>
    </TableRow>

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

    <TextView
        android:id="@+id/lbl_password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_x="21px"
        android:layout_y="131px"
        android:text="Password" >

    </TextView>

    <EditText
        android:id="@+id/txt_password"
        android:layout_width="0dp"
        android:layout_height="47px"
        android:layout_weight="1"
        android:layout_x="53px"
        android:layout_y="155px"
        android:password="true"
        android:textSize="18sp" >

    </EditText>
    </TableRow>

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

        </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    <Button
        android:id="@+id/btn_signin"
        android:layout_width="80pt"
        android:layout_height="wrap_content"
        android:layout_x="52px"
        android:layout_y="200px"
        android:text="SignIn" 
          android:layout_weight="0.025">
    </Button>

    <Button
        android:id="@+id/btn_reset"
        android:layout_width="80pt"
        android:layout_height="wrap_content"
        android:layout_x="123dp"
        android:layout_y="201px"
        android:text="Reset" 
          android:layout_weight="0.025">
    </Button>
    </LinearLayout>
    </TableRow>

</TableLayout>

Output:

enter image description here