2

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

Naruto
  • 9,476
  • 37
  • 118
  • 201

1 Answers1

5

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • @damon, Thanks it worked for me Thanks lot, last thing, what does "Layout_weight" does? it is not reflecting any change in my code if i give value any thing apart from 1 – Naruto May 07 '11 at 12:02
  • It gives it a relative weight to the component relative to the other ones. For example, if you gave one of the buttons a weight of 2, it would get two thirds of the space, while the other one (with a weight of 1) would get a third of the space. – dmon May 07 '11 at 12:50
  • 1
    More specifically, weight is used to distribute any *leftover* space after all children in a LinearLayout have been measured using the usual width or height specifiers. This is why in many examples you will see a width or height (which one depends on the LinearLayout's orientation) set to "0dip" along with a weight - this tells LinearLayout to use only the weight when measuring. If you want two elements to each get exactly 50% of the available space you should use this method. – adamp May 07 '11 at 16:10