Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align a group of views containing a chain in a ConstraintLayout

I have three views in a ConstraintLayout and want to align then like this:

enter image description here

Right now views B and C form a vertical chain, and A is centered relative to the chain. But how do I align the entire group centered in the parent? Note that View C may be GONE.

like image 567
Euro Avatar asked Jun 21 '17 20:06

Euro


People also ask

How do you create a chain in ConstraintLayout?

Creating a chain is really easy, we can click and drag to select views or press ctrl and select views from Component Tree. And then right click on selected views in Editor Or Component Tree to apply constraints. You can see that in action in following screen records.

Can we use linear layout in ConstraintLayout?

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.

Is a group of views that are linked together with bidirectional constraints?

A chain is a group of views linked together with bi-directional positional constraints. The aim of chains is to control the space between views and how the views use the space inside a layout. We have vertical and horizontal chains.

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.


1 Answers

Here is a visual answer without nesting layouts.

enter image description here

Steps

  1. Chain and pack B and C vertically
  2. Chain and pack A and C horizontally
  3. Align B and C horizontal centers
  4. Center A vertically

XML layout

<?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:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="69dp"
        android:layout_height="67dp"
        android:background="#fb0000"
        android:gravity="center"
        android:text="A"
        android:textColor="#000000"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="154dp"
        android:layout_height="73dp"
        android:background="#2000ff"
        android:gravity="center"
        android:text="B"
        android:textColor="#ffffff"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="@+id/textView3"
        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="187dp"
        android:layout_height="61dp"
        android:background="#f1a500"
        android:gravity="center"
        android:text="C"
        android:textColor="#000000"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>

Personal opinion

Switch to Flutter. The layout is much easier than ConstraintLayout.

like image 85
Suragch Avatar answered Sep 23 '22 03:09

Suragch