Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ConstraintLayout : Three buttons equally distribute across the width

Tags:

I have a ConstraintLayout that contains 3 buttons horizontally. I want the 3 buttons to have a fixed width and be evenly distributed across the width of the layout.

like image 906
Atif Amin Avatar asked Feb 13 '17 13:02

Atif Amin


People also ask

Is it possible to evenly distribute buttons across the width of an Android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

How do you put a space between two buttons horizontally on Android?

Notice the line that says android:layout_marginTop="10dip" which ensures that you leave a reasonable 10 dip space in between your buttons. Ofcourse, you can increase (or decrease) that space in between your buttons. That's your choice. Hope this answered your question satisfactorily.

Which is better RelativeLayout or ConstraintLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

How do you put a space between buttons in linear layout?

good answer. Small improvement: use a <Space android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" > </Space> instead of a <View> to make your objective clearer in your XML.


Video Answer


2 Answers

Here is a visual example.

  1. Select the views
  2. Right click and choose Chain > Create Horizontal Chain

enter image description here

See also

  • ConstraintLayout: pack vs chain
like image 137
Suragch Avatar answered Oct 08 '22 01:10

Suragch


To make 3 view equally distribute across the width just need to set constraint start/end of each view must define correct

Code

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button1"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2"
        />

</android.support.constraint.ConstraintLayout>

Output

MORE
If you need 3 views full width, just need to change android:layout_width="wrap_content" to android:layout_width="0dp"

Output

like image 37
Linh Avatar answered Oct 08 '22 03:10

Linh