I need to have a single-line TextView centered inside it's parent ConstraintLayout, but not overlap other views inside that same layout so that it ads ... ellipsis at the end
ConstraintLayout (height: 48dp, width: match_parent) which contains the following 3 child views:
- Left anchored container (height: 48dp, width:variable - green in the image below)
- TextView (centered in ConstraintLayout)
- Right anchored container (height: 48dp, width:variable - blue in the image below)
The left/right containers don't have equal widths (one can be 0 while the other 48dp, or one 48dp and the other 96dp)
XML layout looks like this:
<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="48dp"
android:background="#FF0000">
<LinearLayout
android:id="@+id/container_start"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00FF00"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/container_start"
app:layout_constraintEnd_toStartOf="@id/container_end"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:maxLines="1"
android:ellipsize="end"
android:text="Title Title Title Title" />
<LinearLayout
android:id="@+id/container_end"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#0000FF"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- If I anchor the TextView to the parent (left/right), it will be centered, but text will overlap side containers
- If I anchor the TextView to the side containers, text won't overlap but then it might not be centered if containers have different widths.


