Reputation: 2707
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?
Upvotes: 10
Views: 12611
Reputation: 724
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.
Upvotes: 2
Reputation: 603
You can use this (also add paddings or margins according your design)
<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"/>
Upvotes: 0
Reputation: 365
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"/>
Upvotes: 13
Reputation: 153
Try using a fixed length "layout_width" rather than "wrap_content" this will solve your problem.However I recommend you to align the TextView to left and right of the images and price respectively.
Upvotes: 0
Reputation: 419
try this :
<TextView
android:id="@+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="52dp" //change here the distance between text and icon
app:layout_constraintLeft_toRightOf="@id/iconIv"
app:layout_constraintRight_toLeftOf="@id/priceFl"
app:layout_constraintTop_toTopOf="parent"/>
Upvotes: 0