Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put two buttons on same line in Android

How can I place two buttons on the same line in my login layout on my Android application?

like image 874
user1314147 Avatar asked Apr 17 '12 05:04

user1314147


People also ask

How do I fix the bottom button on my android?

Please take one Relative layout under your main layout . Set its height and width as fill parent and set its gravity as bottom and put any textview or any button you want in it.

What does layout weight do?

Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

How many types of buttons are there in android?

In android, we have a different type of buttons available to use based on our requirements, those are ImageButton, ToggleButton, RadioButton. In android, we can create a Button control in two ways either in the XML layout file or create it in the Activity file programmatically.


4 Answers

Just make a linear layout.Set the orientation to horizontal and add two buttons.Its ready you will get what you want.Before posting such questions try googling you will get the answer for sure.This piece of code will help you.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

if you want to fit the two buttons in the layout.give the weight as 1 for both the buttons.

like image 82
Sreedev Avatar answered Oct 07 '22 11:10

Sreedev


The best solution is to place 2 buttons (Having equal width) in LinearLayout.

One more thing, If you want equal "width" buttons then take button with 0dp width and same weights to all the buttons.

And if you want equal "hight" buttons then take button with 0dp height and same weights to all the buttons.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
like image 33
Paresh Mayani Avatar answered Oct 07 '22 11:10

Paresh Mayani


Use this put two button on the same line....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

   >
    <Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:layout_alignParentBottom="true"
    />
      <Button
    android:id="@+id/cancel"     
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"
    android:layout_toRightOf="@+id/login"
    android:layout_alignParentBottom="true"
    />
      </RelativeLayout>
like image 5
Nitesh Khosla Avatar answered Oct 07 '22 11:10

Nitesh Khosla


You need to add Linear Layout(Horizontal). then you can add many button in one line....
You can also use Relative layout for this.
Here is code for you...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

like image 4
Sandip Armal Patil Avatar answered Oct 07 '22 12:10

Sandip Armal Patil