user1314147
user1314147

Reputation: 362

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?

Upvotes: 14

Views: 61186

Answers (8)

Kumar Santanu
Kumar Santanu

Reputation: 701

You can use linear layout with horizontal orientation and add your two buttons in it :-

  <LinearLayout
    android:id="@+id/btn_action_wallet_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_below="@id/ll_total_coin_container"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">



    <Button
        style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lato_black"
        android:layout_weight="2"
        android:text="Redeem" />

    <Space
        android:layout_width="20dp"
        android:layout_height="wrap_content" />

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Earn More"
        android:layout_weight="2"
        android:fontFamily="@font/lato_black" />


</LinearLayout>

Run it and see how it work

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

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>

Upvotes: 12

Nitesh Khosla
Nitesh Khosla

Reputation: 875

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>

Upvotes: 5

Manjunath
Manjunath

Reputation: 2063

If you are placing the buttons inside the LinearLayout, give the Orientation value as "Vertical", it will automatically place the buttons in the same line. If you are using RelativeLayout, then for one button use android:layout_toLeftOf OR android:layout_toRightOf and give value as ID of other button. If you have got it right, please mark it as the answer. Thanks...

Upvotes: 1

UVM
UVM

Reputation: 9914

I think you need to use RelativeLayout.You can do something like this:

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

    <Button

        android:text="@+id/Button01" 
        android:id="@+id/Button01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true">
    </Button>

    <Button

        android:text="@+id/Button02" 
        android:id="@+id/Button02"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true">
    </Button>

</RelativeLayout>

Also you can refert this as well. http://www.mkyong.com/android/android-relativelayout-example/

Hope this will help you.

Upvotes: 2

Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

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>

Upvotes: 4

vipin
vipin

Reputation: 3001

you can use one linear layout with horizonatal orientation and add your two buttons in it

<LinearLayout

   <Button1.../>
   <Button2.../>
</LinearLayout>

Upvotes: 2

Sreedev
Sreedev

Reputation: 6653

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.

Upvotes: 35

Related Questions