Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a pair of Views inside a ConstraintLayout?

How do I center a pair of Views within a ConstraintLayout as below, without nesting? They need to be touching, and centered as a single unit.

enter image description here

I tried anchoring them to each other and their parent but then they're not touching each other so I added a horizontal bias for both but it didn't help.

Thanks in advance...

<ImageView
    android:id="@+id/imageView"
    android:layout_width="14dp"
    android:layout_height="14dp"
    android:scaleType="fitXY"        
    android:src="@drawable/checkmark"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/textView"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/some_text"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@id/imageView"
    app:layout_constraintRight_toRightOf="parent"/>
like image 341
Barry Fruitman Avatar asked Jul 05 '18 21:07

Barry Fruitman


1 Answers

You need to set a chainstyle attribute for the head of the chain which is the first View on the left in a horizontal chain. The style that will give the expected result is packed. A bias will not be needed in this case.

<ImageView
    android:id="@+id/imageView"
    android:layout_width="14dp"
    android:layout_height="14dp"
    android:scaleType="fitXY"
    android:src="@drawable/checkmark"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/textView"/>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/some_text"
    app:layout_constraintLeft_toRightOf="@id/imageView"
    app:layout_constraintRight_toRightOf="parent"/>
like image 91
Pawel Laskowski Avatar answered Sep 25 '22 22:09

Pawel Laskowski