Pramod Yadav Pramod Yadav - 28 days ago 10
Android Question

Display three button horizontally

I am displaying 3 buttons horizontally,

enter image description here

My code is working fine

<LinearLayout
android:id="@+id/button_layout"
android:layout_below="@id/acll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/approve_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/verify_d"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/delete_d"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<Button
android:id="@+id/modify_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/modify_d"/>
</RelativeLayout>
</LinearLayout>


I am using
RelativeLayout
inside a
LinearLayout
, and then putting button inside
RelativeLayout
, if I use only
LinearLayout
with
weightSum
then all 3 button occupies whole width.

My question: Please let me know any other way to achieve the same task without using nesting layouts

Answer Source

Hi test this code i just have wrote it :

     <LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/acll"
        android:orientation="horizontal">


        <Button
            android:id="@+id/approve_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:text="@string/verify_d" />

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/delete_d" />

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1" />

        <Button
            android:id="@+id/modify_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:text="@string/modify_d" />
    </LinearLayout>