user3622142
user3622142

Reputation: 370

Button side by side LinearLayout

Hello guys I am trying to make the buttons side by side but I cant seem to get it work. I really tried messing around and tried google but failed.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/name" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nameText" />

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="right" />

How would I do it guys?

Upvotes: 0

Views: 1979

Answers (3)

NelsonRoberts
NelsonRoberts

Reputation: 156

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"
android:orientation="vertical"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/name" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nameText" />

<LinearLayout
    android:orientation="horizontal"
    android:height="wrap_content"
    android:width="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_weight="1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_weight="1" />

</LinearLayout>

</LinearLayout>

Upvotes: 1

Aakash
Aakash

Reputation: 5251

You are using linear layout and if you want the buttons side by side then you have to use another linear layout inside your outer linear layout and give it android:orientation="horizontal" Also put the buttons inside this linear layout.

<LinearLayout
 android:orientation="horizontal"     .........>
<button1....>
 <button2....>
  <LinearLayout/>

Upvotes: 2

ucsunil
ucsunil

Reputation: 7494

LinearLayout takes an eldest child wins approach. In your case, you have a LinearLayout with vertical orientation - so you will have the widgets placed one below the other. What you can do is have another nested LinearLayout within your root LinearLayout having horizontal orientation and have both buttons inside this. This would look something like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/name" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nameText" />

<LinearLayout
    android:orientation="horizontal"
    android:height="wrap_content"
    android:width="match_parent" >

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

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

</LinearLayout>

</LinearLayout>

This should get both those buttons side by side. How much space you want to allocate to each button when they are placed side by side is for something for you to figure out.

Notice, I took out

android:layout_gravity="right"

from the second button as it will automatically be placed to the right of the first button

Upvotes: 1

Related Questions