1

I'm trying to create a message layout in a chat application.

This message layout should contain:

  • Message Textview in a rounded rectangle
  • Message status check icon ImageView within that same rectangle
  • Message send time TextView outside the rounded rectangle and to its right.

When text is short, the whole layout should align itself to the right. When the text is long, message TextView is going to expand itself to the left and upon touching the left side of the screen, it expands to a new row.

The problem is: Rounded rectangle stretches to the whole screen regardless of the message being short or long although underlying FrameLayout width is set to wrap_content.

Here is an image of the problem:

enter image description here

The layout I am using is:

<RelativeLayout
    android:id="@+id/outer_rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingTop="8dp">

    <TextView
        android:id="@+id/text_message_time"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:textSize="10sp"
        app:showDate="@{message.postedAt}" />

    <RelativeLayout
        android:id="@+id/bubble_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/text_message_time"
        android:background="@drawable/rounded_rectangle_bubble_sent"
        android:padding="8dp">

        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="4dp"
            app:setStatusPng="@{message.status}" />

        <TextView
            android:id="@+id/messagetext_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="2dp"
            android:layout_toLeftOf="@id/iv_check"
            android:singleLine="false"
            android:text="@{message.messageText}"
            android:textColor="#ffffff" />

    </RelativeLayout>
</RelativeLayout>

The inner RelativeLayout named bubble_ll has its width equals wrap_content.

What I am expecting is:

  • text_message_time TextView should be attached to the right of the screen due to its layout_alignParentEnd property set true.
  • bubble_ll stands left of the text_message_time view, due to its layout_toLeftOf property set. And it should have a width of its contents.

I have tried other layouts such as Linear, ConstraintLayout etc.

The LinearLayout approach did not work, since the width is set to wrap_content for TextView always overlaps status info checks and message time make them disappear when the message in the TextView is long.

With ConstraintLayout I could not align bubble to stay still on the right. It stays on the center of the screen horizontally since it is constrained on both horizontal sides of the screen.

Any help will be appreciated.

Onik
  • 19,396
  • 14
  • 68
  • 91
Mehmet Katircioglu
  • 1,200
  • 1
  • 11
  • 20
  • 1
    You shouldnt be using relative layout anymore.. Use constraint layout. Youre problem with constraint layout is solvable with horizontal_bias from constraint layout. – Archie G. Quiñones Oct 07 '18 at 02:36
  • Use a 9-patch image as a background of your message. Not a layout. – Ümañg ßürmån Oct 07 '18 at 03:34
  • @ArchieG.Quiñones With constraint layout horizontal_bias, I dont know what I should set as bias. I want text to be right aligned all the times. But at the same time, when text is too long, TextView expanding must stop when the left of TextView touches the left side of the screen. So should I set it 0.9? I would prefer to use Constrained Layout, but couldn't manage to work it the way I want. I would be glad if you helped me out. – Mehmet Katircioglu Oct 07 '18 at 11:49
  • @UmangBurman you see there is also an ImageView inside the message box, so I thought I needed a container. The problem is container width matches the parrent somehow. The drawable file I used as background is for just drawing corners. – Mehmet Katircioglu Oct 07 '18 at 11:53
  • 1
    There no need to think about all those things that your talking about when you set your Constraints correctly. To align your text to the right just set your bias to 1.0. – Archie G. Quiñones Oct 08 '18 at 01:40

1 Answers1

1

I have come up with the following layout which is a combination of RelativeLayout and LinearLayout. I hope this serves your purpose. Please change the drawables and the ids if necessary.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outer_rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="8dp">

    <TextView
        android:id="@+id/text_message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/message_container"
        android:layout_alignParentRight="true"
        android:layout_margin="4dp"
        android:text="10:30 am"
        android:textSize="10sp" />

    <LinearLayout
        android:id="@+id/message_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"

        android:layout_toLeftOf="@+id/text_message_time"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/messagetext_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:padding="4dp"
            android:singleLine="false"
            android:text="You have a long message here. This is so long that it takes two lines inside the TextView. No wonder, now its taking three lines and now its four. How long this message can go? I now have that question!!!"
            android:textColor="@android:color/white" />

        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            android:src="@android:drawable/presence_online" />
    </LinearLayout>
</RelativeLayout>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Unfortunately as I mentioned in the question, TextView being wrap_content, dominates the ImageView space when text is too long. ImageView is no longer shown inside the bubble when text is long. – Mehmet Katircioglu Oct 07 '18 at 11:42