Android button layout - get two buttons side by side across the screen

I have two buttons that I would like to show side by side horizontally, but together they fill the horizontal length of the phone. Height is the contents of the wrap, it’s beautiful. My problem right now is that only one button appears (stretches across the screen).

Here is my XML code:

<LinearLayout android:id="@+id/page_buttons" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/prevButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Previous" /> <Button android:id="@+id/nextButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Next" /> 

+7
source share
8 answers

Modify your XML buttons to include the layout_weight attribute:

 <Button android:id="@+id/nextButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Next"/> 
+24
source

Since no one explains why their solutions work, I will give it back.

The problem is the location of the buttons. The two relevant attributes are layout_width and layout_weight.

In other layout systems, when you specify that each layout element should fill in the parent element (layout_width = "fill_parent"), they do this by distributing equal parent space between them. Thus, each of them will have the same size.

Instead, in the Android layout system, if both elements have layout_width = "fill_parent" the first element (in your case, the Preview button) will be stretched to fill the parent element, and the second (or third, etc.) will not leave room for spread, therefore, will not be visible.

Now, in order to understand that you want to show both buttons, you set layout_weight for each button. For the buttons to have the same size, set the same layout weight for both of them.

The layout_definition determines how many β€œparts” (or segments) of the parent each button occupies. The parent will be split into several segments equal to the sum of the child segments. If you want to make one button three times larger than the other, you must assign it the number of parts equal to the number of parts of the first button multiplied by three.

So, if you want your Next button to be twice as large as Previews, you can do this:

  • for the Previews button: layout_weight = 1
  • for the Next button: layout_weight = 2

As a result, the parent will be cut into 3 parts, 2 of which will be highlighted to the Next button and 1 to the Previews button.

The example given here is for buttons and horizontal layout, but this will work great for any type of object, as well as for the parent of the vertical layout.

+13
source
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/space"/> <TextView android:id="@+id/space" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <Button android:id="@+id/button02" android:layout_toRightOf="@id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true"/> </RelativeLayout> 

It looks like you want two identical buttons, not wrapped content. I created a centered pad using a TextView and aligned it with respect. Left button for parent left and spacers, Right button for left button and parent right.

+5
source

The parent of your button is Linear-Layout, and you set the width of the button as the fill of the parent, and therefore take up all the space. You have two options for implementing this ...

  • Give a fixed width for your button (e.g. 50dip).
  • Give the width as 0dp and insert one more attribute in both weight buttons and give 0.5dip for each ...
+1
source

Two buttons should be in linear mode. The line layout should be horizontal, and each button has a weight of 1. This should give you two buttons with the same size across the width of the screen. Sample here. Horizontal orientation: check 2 buttons at the end of this xml layout

+1
source

Your requirements

 <LinearLayout android:id="@+id/page_buttons" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/prevButton" android:layout_width="0dp" android:weight="0.5" android:layout_height="wrap_content" android:text="Previous" /> <Button android:id="@+id/nextButton" android:layout_width="0dp" android:weight="0.5" android:layout_height="wrap_content" android:text="Next" /> 
0
source
 <?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="wrap_content" android:layout_centerHorizontal="true"> <TextView android:text="@+id/SomeText" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:background="@android:drawable/bottom_bar" android:paddingLeft="4.0dip" android:paddingTop="5.0dip" android:paddingRight="4.0dip" android:paddingBottom="1.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/TextView01"> <Button android:id="@+id/allow" android:layout_width="0.0dip" android:layout_height="fill_parent" android:text="Allow" android:layout_weight="1.0" /> <Button android:id="@+id/deny" android:layout_width="0.0dip" android:layout_height="fill_parent" android:text="Deny" android:layout_weight="1.0" /> </LinearLayout> </RelativeLayout> 
0
source

I found the main problem for people who still don't seem to get what they want. when using buttons with its usual (outside the box / default) background, it has a built-in marker. If you try to change the background manually in one color, you can easily say that you just need to set a separate background. other than that, when you have weights, they will work.

0
source

Source: https://habr.com/ru/post/896241/


All Articles