I have a constraint layout and when the string is long enough it overlaps the element on the right. Sample code is:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iconIv"
android:layout_width="36dp"
android:layout_height="36dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:id="@+id/priceFl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
...
Then I tried to nameTv to add right constraint to left of the element on the right like this app:layout_constraintRight_toLeftOf="@id/priceFl"
and this happened:
Is there a way to position this text between icon and price views?
Try this
<TextView
android:id="@+id/nameTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintRight_toLeftOf="@+id/priceFl"
app:layout_constraintTop_toTopOf="parent"/>
Try this, the width is 0dp
and using app:layout_constraintHorizontal_weight
you can adjust the width as per your requirement.
<ImageView
android:id="@+id/iconIv"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/nameTv"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/nameTv"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="2"
android:layout_height="wrap_content"
android:text="Some text"
app:layout_constraintStart_toEndOf="@+id/iconIv"
app:layout_constraintEnd_toStartOf="@+id/priceFl"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/priceFl"
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/nameTv"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
I prefer using constraintStart
and constraintEnd
instead of constraintLeft
and constraintRight
, just a personal choice.
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