Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView background does not wrap content

I have a TextView inside ConstraintLayout with a background drawable. It's width is set to 0dp (match_constraint). If I set it to wrap_content, the text clips at the end.

I want the background to be the size of TextView only.

This is how I'm getting it

XML:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:layout_height="match_parent">

    <TextView
        android:id="@+id/message_receive"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/rounded_chat_message"
        android:padding="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="This is a received message"/>

</androidx.constraintlayout.widget.ConstraintLayout>

After setting it to wrap_content:

enter image description here

Edit:

rounded_chat_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">

            <gradient android:angle="0"
                android:startColor="@android:color/darker_gray"
                android:endColor="@android:color/darker_gray" />
            <corners android:radius="16dp" />
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="16dp" />
        </shape>
    </item>
</layer-list>
like image 299
RandomyzeEverything Avatar asked Sep 06 '25 03:09

RandomyzeEverything


1 Answers

To prevent the TextView from clipping when using wrap_content to need to set the app:layout_constrainedWidth="true" attribute. This will enforce the horizontal constraints when the width is set to wrap_content so both start and end have to be set (which they are in your case). Now to make the TextView stick to the start you need to set app:layout_constraintHorizontal_bias="0" (or 1 to align to the end).

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:layout_height="match_parent">

    <TextView
        android:id="@+id/message_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/rounded_chat_message"
        android:padding="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Subtitle"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="Message"/>

</androidx.constraintlayout.widget.ConstraintLayout>
like image 122
Pawel Laskowski Avatar answered Sep 07 '25 23:09

Pawel Laskowski