0

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?

3 Answers 3

2

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/> 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1
<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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.