Hello I am trying to put two buttons next to eachother in android studio with linearlayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="222dp"
android:layout_height="237dp"
app:srcCompat="@drawable/ic_launcher_background"
tools:layout_editor_absoluteX="246dp"
tools:layout_editor_absoluteY="322dp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/cameraBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/camera"
android:layout_marginTop="8dp"
app:strokeColor="@color/teal_700"
app:cornerRadius="100dp"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/galleryBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_marginTop="8dp"
app:cornerRadius="100dp"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
/>
the output currently is:
I have tried with relativelayout – didn’t work and also using horizontal in linearlayout.
I have tried putting also the android:layout_weight as 1 and the buttons disappeared.
I want to have the imageview in the center and the two buttons below and the two buttons are next to each other.
5
Answers
If you are intending to keep this view inside of a LinearLayout this is how you will do it. You will add a new LinearLayout below your ImageView and set its orientation or horizontal. Then add your buttons inside of that LinearLayout.
If you are interested in using ConstraintLayout this is how you would do it:
You can use another
Linear Layout
inside the parent layout and set its orientation to horizontal to achieve the desired output.Code For Desired Output:
In your current layout, all the elements are in one container, that is your LinearLayout.
One idea to achieve what you want with a LinearLayout is to separate the image and buttons in 2 different LinearLayouts.
The orientation of the LinearLayout that will host your buttons should be horizontal.
If the LinearLayout isn’t a hard requirement, I would suggest to use ConstraintLayout. By using that, you can position everything in a much better and easy way.
You need to use another linear layout to put two button besides each other,
Here is the code for this.
Here is the screenshot of the desired output.
you can give desired margins to set the buttons in the layout.
The outer (main) layout has a vertical orientation, so all the elements inside this layout will be arranged vertically, So in order to have these two buttons next to each other you have to use inner
linearlayout
(inside the main layout) and set its ‘orientation’ to horizontal.