Reputation: 366
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.
Upvotes: 0
Views: 80
Reputation: 96
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.
Upvotes: 1
Reputation: 763
Edit your layout to be like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D7D7D7">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/bot_icon"
android:layout_width="50dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_botchat"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/bot_message"
android:elevation="3dp"
android:fontFamily="@font/roboto"
android:maxLines="2"
android:paddingStart="16dp"
android:paddingTop="10dp"
android:paddingEnd="16dp"
android:paddingBottom="10dp"
android:text="Lorem Ipsum."
android:textColor="@color/visia"
android:textSize="18sp"
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.constraintlayout.widget.ConstraintLayout>
Then create XML file "bot_message.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="0dp"
android:topRightRadius="20dp" />
<solid android:color="#F5F5F5" />
</shape>
Upvotes: 0
Reputation: 847
If your question is about how to remove the right space in the textView
, simply remove this line in your textview
attributes:
app:layout_constraintEnd_toEndOf="parent"
You are actually bounding the end of your textview
to the end of its parent which is why wrap_content
is not functioning well. So the result would be:
<?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/boy_icon"
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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="@drawable/round_gray_20"
android:elevation="3dp"
android:paddingStart="16dp"
android:paddingTop="10dp"
android:paddingEnd="16dp"
android:paddingBottom="10dp"
android:text="fsdfsdfsdfsdsfsd "
android:textColor="@color/blackText"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Note that I may have changed some drawables
and have used mine to be able to see the result. So revert them to the original ones accordingly.
Upvotes: 0
Reputation: 587
try to put a width for the text parent and put the text wrap_content
Upvotes: 0
Reputation: 1
use the attribute "maxLines" to specify the number of lines your textview should have. For example,
android:maxLines="2"
Upvotes: 0