Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear Layout with two buttons side by side - android

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

like image 839
Devester Avatar asked Feb 15 '13 10:02

Devester


People also ask

How do I change the position of a button in linear layout?

You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and right for each. Save this answer.

Which XML attribute will you use to show views side by side in a linear layout?

android:orientation="horizontal" is the XML attribute used in LinearLayout to show views side by side.

What is linear in android?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.


2 Answers

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

like image 88
Abhinav Singh Maurya Avatar answered Sep 17 '22 16:09

Abhinav Singh Maurya


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

like image 34
user3287335 Avatar answered Sep 20 '22 16:09

user3287335