15

I have 3 buttons the layout.xml below where they appear below of each other...

                <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="10dip" >

                    <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <Button
                        android:id="@+id/btn_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Car" />

                    <Button
                        android:id="@+id/btn_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Vehicle" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/btn_3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Bike" />
                </LinearLayout>
            </LinearLayout>

I would like to have the first two buttons side by side (btn_1 and btn_2). Could anybody give me a hint about how to do that???

Thanks a lot

Devester
  • 1,183
  • 4
  • 14
  • 41
  • is the same case: http://stackoverflow.com/questions/5551349/2-buttons-side-by-side-android-layouts/21411490#21411490 – iarroyo Jan 28 '14 at 16:48

3 Answers3

19

just change android:orientation="vertical" to android:orientation="horizontal" of your layout and every thing will work fine

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
9

best way to do this is to make layout and then put your button like this code

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

 <Button 
    android:id="@+id/Button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:text="@string/Home1"/>  

 <Button 
    android:id="@+id/Button11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"     
    android:text="@string/NextL"/>

in this shape you have two button in same row so easy :D

user3287335
  • 91
  • 1
  • 1
  • Seems like you forgor a closing tag? Also, "A TableRow should always be used as a child of a TableLayout", according to the [docs](http://developer.android.com/reference/android/widget/TableRow.html). – dst Feb 17 '14 at 20:48
  • yea i forgot those, and no it is not an obligation they could be used inside most of the layouts , i used them inside a LinearLayout and that work out – user3287335 Feb 22 '14 at 20:57
  • that's why the docs only mention *should* and not *must*. But as the docs recommend against it, I would not recommend it either in case new Android versions break something there. – dst Feb 23 '14 at 14:08
3

Change the Linear Layout orientation Vertical into Horizontal.And then Give weight for your both buttons as 1 or 2.As your wish.the buttons will be arranged equally.

Murugan
  • 31
  • 2