Search code examples
androidxmlkotlinandroid-layoutandroid-relativelayout

How to constraint a relative layout to take maximum of specified width on screen


In my layout file, I have a relative layout and in that layout , I have a textView. If the text is very large, I want it to take only 60% of the screen and rest of the text will go to new line.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/rounded_corner"
        android:layout_alignParentEnd="true">

        <TextView
            android:id="@+id/txtSentMsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is sent message"
            android:textColor="@color/white"
            android:textSize="18sp"

            />
    </RelativeLayout>
</RelativeLayout>

This is my layout file and the pictures attached will clarify my doubt.

enter image description here

as you can see my sent message takes up the whole screen. I want the layout to be aligned to the right and also take up maximum of 60% of the screen.

UPDATE :

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:animateLayoutChanges="true"
    android:layout_marginTop="4dp"
    android:padding="4dp"
    android:layout_height="wrap_content">


    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        app:layout_constraintWidth_max="wrap"
        android:textColor="@color/white"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/txtSentMsg"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.7"
        tools:text="Hello, this is a long message and will use other lines as i grows. The default width is 80% of any screen."
        />


</androidx.constraintlayout.widget.ConstraintLayout>

Now i have used this and it works. I gives me maximum of 60% screen area But now i can see sometimes, the text view takes up whole of 60 % when contents in it are less.

enter image description here

As you can see the text how are you is taking up whole 60%. This space goes out when i close the activity and start it again and appears randomly in any message.


Solution

  • I would suggest usage of linear layout. It is the fastest one, and you could achieve what you want in, on my opinion, easy way:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:weightSum="1">
    
        <LinearLayout
            android:layout_weight="0.6"
            android:layout_width="0dp"
            android:gravity="end"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/txtSentMsg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="This is a very long message that will go in two lines"
                android:background="@color/colorAccent"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>