'How to make content center of horizontally equally divided views in ConstraintLayout android
I need to divide 3 views equally horizontally and the content of the views to be centered. I tried using the link: https://medium.com/@nomanr/constraintlayout-chains-4f3b58ea15bb
But I'm unable to make the views center and equally divided.
I also tried using app:layout_constraintHorizontal_weight but that didn't work. Then I tried using chain which is given below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="81dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/view2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_8"
android:text="Name"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="@color/primaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvINameValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_1"
android:layout_marginBottom="@dimen/dp_4"
android:text="Sachin"
android:textSize="@dimen/text_size_16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvName" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_1"
android:layout_marginBottom="@dimen/dp_4"
android:text="Dey"
app:layout_constraintStart_toEndOf="@id/tvINameValue"
app:layout_constraintTop_toBottomOf="@id/tvName" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/viewDividerLeft"
android:layout_width="@dimen/dp_1"
android:layout_height="wrap_content"
android:background="@drawable/divider_blue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/view1"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2"
app:layout_constraintRight_toLeftOf="@+id/view3"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintLeft_toRightOf="@+id/view1"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvRollNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceBody2"
android:text="Roll No"
android:textColor="@color/primaryDark"
android:layout_marginTop="@dimen/dp_8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvRollNoValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size_16"
android:text="123"
android:layout_marginTop="@dimen/dp_1"
android:layout_marginBottom="@dimen/dp_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRollNo"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/viewDividerRight"
android:layout_width="@dimen/dp_1"
android:layout_height="wrap_content"
android:background="@drawable/divider_blue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/view2"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@+id/view2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_8"
android:text="Address"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="@color/primaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvAddressValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_1"
android:layout_marginBottom="@dimen/dp_4"
android:text="Titan 123"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvAddress" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
You actually are on the right path , You need Chains in Constraint Layout..
- Place the buttons(without constraints) and then select Chains>Create Horizontal Chains..(Also there is further option to customize your chains , packed, scattered etc)
- Again select the 3 elements and align top.
Make sure that if you're trying to chain.. There isn't any existing constraint on them
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
