I have to implement a chat bot, but i don't know how to size the textview for the messages.
I want that the ballon exactly fit the text, without the white blank space on the right, without using maxWidth
.
My code is
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/bot_icon"
android:layout_width="50dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/ic_botchat"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@id/bot_icon"
app:layout_constraintTop_toBottomOf="@id/bot_icon">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="@drawable/bot_message"
android:elevation="3dp"
android:fontFamily="@font/roboto"
android:paddingStart="16dp"
android:paddingTop="10dp"
android:paddingEnd="16dp"
android:paddingBottom="10dp"
android:textColor="@color/visia"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried to modify the android:layout_width
in the AppCompatTextView with wrap_content
, but the text is visualised in one line, going out of the screen.
According to this answer, you can not achieve what you want by using the default TextView and you need to have a custom text view where you override the onMeasure method so that the maximum width will be the width of your largest line.