Search code examples
androidandroid-layoutbuttonandroid-linearlayoutandroid-relativelayout

How do I place buttons next to each other, sticking them, in Android Layout


I am new to Android UI and I am trying to build a UI like this - The problem I am facing is positioning the buttons like this.UI Wireframe


Solution

  • If you want to put your button in center you need to use android:layout_gravity="center"

    SAMPLE CODE

    <LinearLayout
            android:layout_width="250dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="@drawable/linear_bg"
            android:orientation="horizontal">
    
        <com.google.android.material.button.MaterialButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:backgroundTint="@android:color/transparent"
                android:text="Login" />
    
        <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/white" />
    
        <com.google.android.material.button.MaterialButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:backgroundTint="@android:color/transparent"
                android:text="Sign Up" />
    
    </LinearLayout>
    

    drawable/linear_bg

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
        <solid android:color="#3F51B5" />
        <corners android:radius="20dp" />
    </shape>
    

    OUTPUT

    enter image description here