3

I have one listView , the item contains two component imageView and textView and I am trying to set the text background inside the textView.

So I think if I am able to set the Textview width to wrap_content then it will be fine to use android:background tag.

What I did is set the textview width and height to wrap_content and it works only if I have a short text but If I have a long one some of the text will be out of the screen

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
app:layout_constraintWidth_default="wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp">

<ImageView
    android:id="@+id/imageViewChat"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:srcCompat="@drawable/googleg_standard_color_18"
    android:layout_marginStart="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="26dp"
    android:layout_marginLeft="16dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    android:id="@+id/textViewChat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"

    android:layout_marginRight="16dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="26dp"
    android:background="@drawable/rounded_corner"

    android:fontFamily="serif"
    android:text="ssssss ssssss ssssss ssssss ssssss ssssss  ssssss 
    ssssss  ssssss ssssss "
    android:textAlignment="textStart"
    android:textColor="@color/bb_darkBackgroundColor"
    android:textSize="15sp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/imageViewChat"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>
Mohamed Elloumi
  • 158
  • 4
  • 21
  • Duplicate of [Wrap\_content view inside a ConstraintLayout stretches outside the screen](https://stackoverflow.com/questions/40850966/wrap-content-view-inside-a-constraintlayout-stretches-outside-the-screen) – Silvia H Apr 11 '19 at 15:42

1 Answers1

22

you have to set width to 0dp and the default width behavior as wrap to textview as below

android:layout_width="0dp"
app:layout_constraintWidth_default="wrap"

and also you missing top constraint which will lead to misplace view runtime

Pavan
  • 5,016
  • 1
  • 26
  • 30
  • Thank it works as expected but don"t know why I am not able to to set text alignment after adding this line : app:layout_constraintWidth_default="wrap", usually I am able to set text alignment text start using this : chattext.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END) – Mohamed Elloumi Aug 01 '17 at 15:20
  • not much idea it works in xml you can check for android:gravity="end" or setGravity in java – Pavan Aug 01 '17 at 15:42
  • I found it just need to disable this line programatically if you can help thanks a lot : app:layout_constraintWidth_default="wrap" – Mohamed Elloumi Aug 01 '17 at 15:58
  • if you want your view fixed to right with full device width you can remove app:layout_constraintWidth_default="wrap" from xml but depends on your requirement – Pavan Aug 01 '17 at 16:01
  • it"s a chat like facebook for example some times in the right and sometimes in the left for now every thing is working as expected just need to disable this line app:layout_constraintWidth_default="wrap" effect under my if condition – Mohamed Elloumi Aug 01 '17 at 16:04
  • check this changing/setting constraint run time https://stackoverflow.com/questions/45263159/constraintlayout-change-constraints-programmatically – Pavan Aug 01 '17 at 16:05
  • I have no idea what the layout_constraintWidth_default attribute does but I didn't need it for my TextView, I just needed to change "wrap_content" to 0dp – noobular Aug 25 '17 at 13:37