Create two buttons of equal size inside Linear Layout in Android

Following code we used to create two buttons we use to select the Gender of user during registration,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="@dimen/_10sdp"
    android:weightSum="2"
    android:orientation="horizontal">
    <Button
        android:id="@+id/buttonMale"
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginRight="20dp"
        android:elevation="10dp"
        android:text="Male"
        android:textColor="@color/colorWhite"
        android:fontFamily="@font/sourceserifpro_regular"
        app:icon="@drawable/svg_heart_border"
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:textSize="20dp" />
    <Button
        android:id="@+id/buttonFemale"
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:elevation="10dp"
        android:text="Female"
        android:textColor="@color/colorWhite"
        android:fontFamily="@font/sourceserifpro_regular"
        app:icon="@drawable/svg_heart_border"
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:textSize="20dp" />
</LinearLayout>

As you can see in LinearLayout we have set, android:weightSum=”2″ and then in each of the button we set android:layout_weight=”1″ to make those of equal size, and also another important is android:layout_width=”0dip”

For using Material view for this buttons we have set the theme as,

        app:icon="@drawable/svg_heart_border"
        style="@style/Widget.MaterialComponents.Button.Icon"

Leave a Comment