wegtis
wegtis

Reputation: 333

Chat message box as wide as text content

So i want to have chat message container as wide as the message is. I want to achieve something really similiar to this kind of chat message: http://img.creativemark.co.uk/uploads/images/805/15805/img3File.png

Here is my layout, with which i am struggling:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/single_message_view"
android:layout_margin="5dp">

<LinearLayout
    android:layout_marginEnd="15dp"
    android:layout_marginStart="15dp"
    android:layout_alignParentEnd="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="100dp"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="author"
            android:id="@+id/sender_data"
            android:layout_marginEnd="5dp"
            android:textColor="@color/colorWhite" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:layout_height="wrap_content"
            android:text="date"
            android:textColor="@color/colorWhite"
            android:id="@+id/send_date" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/message_content"
        android:background="@drawable/msg_haze"
        >

        <TextView
            android:layout_margin="10dp"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:id="@+id/message_text"
            android:text="content" />

        <LinearLayout
            android:layout_margin="10dp"
            android:minWidth="100dp"
            android:layout_below="@id/message_text"
            android:id="@+id/tags_container"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:id="@+id/message_tags"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layoutManager="LinearLayoutManager"
                tools:listitem="@layout/item_tag" />
        </LinearLayout>

        <ImageView
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="10dp"
            android:paddingBottom="10dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/message_state" />

        <LinearLayout
            android:id="@+id/attachment_holder"
            android:layout_marginTop="10dp"
            android:padding="5dp"
            android:visibility="gone"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/attachments_haze">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/attachments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layoutManager="GridLayoutManager"/>

            <ImageView
                android:id="@+id/attachment"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Views: 262

Answers (4)

Mit
Mit

Reputation: 318

temp_black.xml (This will be used as a background of the chat box, you can change the color in the below file to #FFFFFF to get the desired output)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#000000"/>

    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />

</shape>

temp_white.xml (This is the black dot between the Name of Sender and Time, i know in the image the color is black, but for reference purpose i have made this, you cant just edit the color in below xml to get the output of your choice)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#FFFFFF"/>

    <corners android:radius="100dip" />

    <padding
        android:bottom="0dip"
        android:left="0dip"
        android:right="0dip"
        android:top="0dip" />

</shape>

temp.xml (The layout file)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_margin="10dp"
        android:background="@drawable/temp_black"
        android:paddingRight="10dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:text="Sample Text Sample Text"
                android:textSize="16sp"
                android:textColor="#FFFFFF"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:text="Name"
                    android:textSize="11sp"
                    android:textColor="#FFFFFF"/>

                <ImageView
                    android:layout_width="5dp"
                    android:layout_height="5dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:adjustViewBounds="true"
                    android:src="@drawable/temp_white"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:text="Time"
                    android:textSize="11sp"
                    android:textColor="#FFFFFF"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Finally the output image enter image description here

Upvotes: 0

Usman Ghauri
Usman Ghauri

Reputation: 941

agree with @basavannevva .. 9 patch image automatically stretches .. you have to provide its content area and stretch area. there are plenty of online tools that can create a 9 patch image. and i think even android studio can edit 9 patch file.

Upvotes: 0

Shabbir Dhangot
Shabbir Dhangot

Reputation: 9121

Usually for this type of chat bubble we need to use the nine patch image. This nine patch image can re-size according to content inside.

Below are nine patch image for send and receive message.

enter image description here

enter image description here

Save this image as .9.png extension. Use this image to give background.

Upvotes: 1

Basavannevva
Basavannevva

Reputation: 294

To display the chat Message you need to use 9 patch images that allows you to create bitmap images that automatically resize to accommodate the contents of the view

Upvotes: 2

Related Questions