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.
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"/>
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"/>
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