How to align horizontally 3 fix width buttons in LinearLayout?

1.1k Views Asked by At

I have this layout:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="32dp">

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button1"/>

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button2"/>

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button3"/>

    </LinearLayout>

I want to align horizontally these 3 fix width buttons. Could you help me please ?

3

There are 3 best solutions below

2
On BEST ANSWER

Basically there are 3 steps involved.

  1. Set weightSum="3" to the parent layout. This means that the sum of the entire layout_weights is 3.

  2. Set layout_weight="1" to each of the individual buttons. So each individual button has 1/3rd the size of the parent.

  3. Finally set layout_width="0dp", this is important because here you don't have to set the width of the view. It will be set automatically by the layout handler.

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button1"
            android:layout_weight="1"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:layout_weight="1"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button3"
            android:layout_weight="1"/>
    
    </LinearLayout>
    
0
On

Try this code

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="32dp">

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button1"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Button2"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button3"/>

</RelativeLayout>
0
On

Try this and change weight according to your requirement

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="9" 
        android:orientation="horizontal"
        android:paddingTop="32dp">
        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <Button
            android:layout_width="0dp"
             android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button2"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button3"/>
    </LinearLayout>