I have a image and a textview next to it in a constraint layout, textview has a really long text and I want fit the text within the text view and not overlap on the image
this is what I have tried
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/tariffTileHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@ id/tariffIconIV"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_electricity_circle_logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tariffDescTV"
style="@style/Text.Large.Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="3"
app:layout_constraintStart_toEndOf="@ id/tariffIconIV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is a very long name to be sue it fits no matter how long it is" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is how it looks
could you suggest what I am missing here please
thanks for your suggestions R
CodePudding user response:
You can use this code to set up the text in the layout.
<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="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@ id/tariffIconIV"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_launcher_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tariffDescTV"
style="@style/Text.Large.Bold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lines="3"
app:layout_constraintStart_toEndOf="@ id/tariffIconIV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is a very long name to be sue it fits no matter how long it is" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the desired output.
CodePudding user response:
Answer
Set android:layout_width="0dp"of textView (tariffDescTV) and width of parent container as match_parent.
Output
(I have used image of cross for example)
Code For the Same
<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:id="@ id/tariffTileHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@ id/tariffIconIV"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_cross"
android:tint="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tariffDescTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lines="3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@ id/tariffIconIV"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is a very long name to be sue it fits no matter how long it is" />
</androidx.constraintlayout.widget.ConstraintLayout>



