How to center align 3 buttons with next to each other vertically using ConstraintLayout
?
To be clear, i want to convert this simple layout structure into flat UI using ConstraintLayout
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </FrameLayout>
I have obtained a nearest solution as follows
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/button2" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" tools:layout_conversion_absoluteHeight="48dp" tools:layout_conversion_absoluteWidth="88dp" tools:layout_conversion_absoluteX="148dp" tools:layout_conversion_absoluteY="259dp" app:layout_constraintBottom_toTopOf="@+id/button3" app:layout_constraintTop_toBottomOf="@+id/button" app:layout_constraintRight_toRightOf="parent"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" tools:layout_conversion_absoluteHeight="48dp" tools:layout_conversion_absoluteWidth="88dp" tools:layout_conversion_absoluteX="148dp" tools:layout_conversion_absoluteY="307dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/button2" app:layout_constraintRight_toRightOf="parent"/> </android.support.constraint.ConstraintLayout>
But clearly, you can see that the obtained output does not match to the required one. i don't want any margin or space in between the 3 buttons, all i want is to center align those 3 buttons next to each other vertically just like they are in a LinearLayout
which has a vertical orientation.
I am quite new to Android development and today I wondered what the 'Vertical Bias' respectively the 'Horizontal Bias' is used for in the 'ConstraintLayout'. Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis".
Following are the differences/advantages: Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).
You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.
It is good that you have created the chain between those 3 views. Having a chain you can specify the chain "style" and it will affect how views are distributed along the chain axis. The chain style can be controlled by "chain" button right below the view:
Click on it few times to toggle between all 3 modes:
spread (the default one)
spread_inside
packed
As you can see - packed
is the one that you want.
Setting the chain style will result in adding following attribute to the first child in the chain, so you can do it also from XML:
app:layout_constraintVertical_chainStyle="packed"
Solution proposed in the other answer may look like it works, but in reality it is not a proper solution for your problem. Consider the case when you have views with different heights, lets say the bottom one is larger. That solution will lock the middle view in center and position other views above and below. It will not result in a "centered group" (only the middle view would be centered). You can see the comparison in the image below:
drag and drop three buttons into the Android Studio's 'Layout Editor'
Select those buttons together by dragging mouse
Pack them vertically using the 'pack' button in 'Layout Editor'
Align them center horizontally using 'Align-Center horizontally' button
Align them center vertically using the 'Align-Center vertically' button
pack all those three buttons into a vertically packed chain using
app:layout_constraintVertical_chainStyle="packed"
add left and right constraints for all those three buttons as parent
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteY="25dp" tools:layout_editor_absoluteX="0dp"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintBottom_toTopOf="@+id/button2" app:layout_constraintTop_toTopOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:layout_conversion_absoluteHeight="48dp" tools:layout_conversion_absoluteWidth="88dp" tools:layout_conversion_absoluteX="148dp" tools:layout_conversion_absoluteY="259dp" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintBottom_toTopOf="@+id/button3" app:layout_constraintTop_toBottomOf="@+id/button1" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:layout_conversion_absoluteHeight="48dp" tools:layout_conversion_absoluteWidth="88dp" tools:layout_conversion_absoluteX="148dp" tools:layout_conversion_absoluteY="307dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/button2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"/> </android.support.constraint.ConstraintLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With