Solution 1 :
Well You are forcing each item to take require width by using android:layout_width="wrap_content"
in each View. If you can fix the width of your ImageView then by using android:maxWidth="XXdp"
in Text view you can achieve what you want.
Code will be as follows –
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="wrap_content"
android_layout_height="wrap_content">
<ImageView
android_id="@+id/cast_image"
android_layout_width="100dp" //here fixing width to 100dp
android_layout_height="wrap_content"
android_layout_marginStart="8dp"
android_scaleType="centerCrop"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
tools_srcCompat="@tools:sample/avatars" />
<TextView
android_id="@+id/cast_name_text"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_maxWidth="100dp" //fixing TextView to max 100dp
android_layout_marginTop="8dp"
android_text="TextView"
app_layout_constraintEnd_toEndOf="@+id/cast_image"
app_layout_constraintStart_toStartOf="@+id/cast_image"
app_layout_constraintTop_toBottomOf="@+id/cast_image" />
<TextView
android_id="@+id/cast_char_text"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginTop="8dp"
android_text="TextView"
app_layout_constraintEnd_toEndOf="@+id/cast_name_text"
app_layout_constraintStart_toStartOf="@+id/cast_name_text"
app_layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
Following xml will result as –
Happy Coding !
Problem :
I have RecyclerView
item with one image and two text views.
If the text is too long, it is causing that one item will be bigger then other item, and overall a bad UI.
What can I do to limit the text to respect the width of another view – the image in that case?(The “three dots” will be proper solution for me)
my layout code for recycler view item:
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="wrap_content"
android_layout_height="wrap_content">
<ImageView
android_id="@+id/cast_image"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginStart="8dp"
android_scaleType="centerCrop"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
tools_srcCompat="@tools:sample/avatars" />
<TextView
android_id="@+id/cast_name_text"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginTop="8dp"
android_text="TextView"
app_layout_constraintEnd_toEndOf="@+id/cast_image"
app_layout_constraintStart_toStartOf="@+id/cast_image"
app_layout_constraintTop_toBottomOf="@+id/cast_image" />
<TextView
android_id="@+id/cast_char_text"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginTop="8dp"
android_text="TextView"
app_layout_constraintEnd_toEndOf="@+id/cast_name_text"
app_layout_constraintStart_toStartOf="@+id/cast_name_text"
app_layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
Image for clarifying the problem –