Search code examples
androidxmlgravity

TextView height stretches in parent with gravity right. why?


I am making chat activity and having issues with right chat bubble. As you can see in the image below. TextView stretches height wise. According to my thinking it should be stretching width wise first then if space is full it should stretch height wise. Please suggest me if it is possible with xml or not. I have seen java code solution but i want to find a solution in xml first.

image

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="20" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="80"
    android:gravity="right">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:background="@drawable/ic_bubble_sent"
        android:gravity="right">

        <TextView
            android:id="@+id/txt_msg_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/txt_message_time"
            android:layout_toStartOf="@+id/txt_message_time"
            android:textSize="16sp"
            android:text="Lorem"
            android:visibility="visible" />

        <TextView
            android:id="@+id/txt_message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txt_msg_content"
            android:layout_marginTop="-9dp"
            android:text="00:33"
            android:textColor="@color/colorAppGray"
            android:textSize="12sp"
            android:textStyle="normal" />
    </RelativeLayout>
    </LinearLayout>
</LinearLayout>

Solution

  • Replace your two TextView with this,

            <TextView
                android:id="@+id/txt_msg_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="Lorem"
                android:visibility="visible"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/txt_message_time"
                android:layout_toStartOf="@+id/txt_message_time"
                android:layout_marginRight="32dp"
                android:layout_marginEnd="32dp" />
    
            <TextView
                android:id="@+id/txt_message_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="00:33"
                android:textSize="12sp"
                android:textStyle="normal"
                android:layout_alignBaseline="@+id/txt_msg_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
    

    I removed all unnecessary alignments from first TextView.

    UPDATE The overall layout looks like,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp">
    
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="20" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="80"
            android:gravity="right">
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                >
    
                <TextView
                    android:id="@+id/txt_msg_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
    
                    android:textSize="16sp"
                    android:text="Lorem"
                    android:visibility="visible"
                    android:layout_alignParentTop="true"
                    android:layout_toLeftOf="@+id/txt_message_time"
                    android:layout_toStartOf="@+id/txt_message_time"
                    android:layout_marginRight="32dp"
                    android:layout_marginEnd="32dp" />
    
                <TextView
                    android:id="@+id/txt_message_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
    
                    android:text="00:33"
    
                    android:textSize="12sp"
                    android:textStyle="normal"
                    android:layout_alignBaseline="@+id/txt_msg_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />
            </RelativeLayout>
    
        </LinearLayout>
    </LinearLayout>
    

    Note : I removed your background for testing.