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>
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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)
not much idea it works in xml you can check for android:gravity="end" or setGravity in java
I found it just need to disable this line programatically if you can help thanks a lot : app:layout_constraintWidth_default="wrap"
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.