androidbuttonandroid-linearlayoutpaddingmargin

Add space between two buttons


I have a LinearLayout with two Buttons in it. The Buttons are touching eachother in the middle of the Layout, i want to add some space between them. They are horizontal orientated and i would like to have the left button on the very left of the layout and the right one at the very right.

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="20dp">


            <Button
                android:id="@+id/truthDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/truthdialogbutton"
                android:text="truth" />

            <Button
                android:id="@+id/dareDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/daredialogbutton"
                android:text="truth"/>
        </LinearLayout>

I´ve tried different thing like, increasing the margin or the padding of the layout and buttons, but nothing really seems to work


Solution

  • Add android:layout_weight="1" in both buttons and give a Space between these buttons. And as you want to place these buttons on the left and right sides of the screen, you should set the width of the LinearLayout to match_parent.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="20dp">
    
    
        <Button
            android:id="@+id/truthDialogButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/truthdialogbutton"
            android:text="Truth" />
    
        <Space
            android:layout_width="60dp"
            android:layout_height="match_parent" />
    
        <Button
            android:id="@+id/dareDialogButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/daredialogbutton"
            android:text="Dare"/>
    </LinearLayout>