Naruto
Naruto

Reputation: 9634

Layout design problem

in android i m trying to make a simple form with two buttons.. but i am facing alignment issue. can you please help in that..

here is the code

  <?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:stretchColumns = "1"
  >
    <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UserName"></TextView>
        <EditText android:text="EditText" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    </TableRow>
    <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Password"></TextView>
        <EditText android:text="EditText" android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    </TableRow>
    <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button1" android:text="save"></Button>
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2" android:text="cancel"></Button>
    </TableRow>
</TableLayout>

enter image description here

with this code,i am getting UI like this,but i want the buttons to be aligned,please help me

Upvotes: 2

Views: 267

Answers (1)

dmon
dmon

Reputation: 30168

Move the buttons out of the table layout and into a LinearLayout?

<LinearLayout android:id="@+id/buttons" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content">
        <Button 
           android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:id="@+id/button1" 
           android:text="save"
           android:layout_weight="1"/>
        <Button 
           android:layout_height="wrap_content" 
           android:layout_width="wrap_content" 
           android:id="@+id/button2" 
           android:text="cancel"
           android:layout_weight="1"/>
    </LinearLayout>

You might have to set both the table and the LinearLayout in a vertical LinearLayout and set weights on them.

Upvotes: 5

Related Questions