How to create a chat activity with the time stamp in bubble
Asked Answered
C

3

1

I want to create a chat activity in which user can send and receive messages. Sender messages on right in bubble with time stamp and received messages on left in buble with time stamp.

What I have done so far is that I have created a chat application its working fine, i get my messages on right and received messages on left but I don't know how to add time stamp in bubble just like whatsapp.

Please help its very important for me.

Chalcidice answered 8/1, 2015 at 10:18 Comment(0)
T
1

use following code to get current time :

 DateFormat df = new SimpleDateFormat("HH:mm");
 Calendar calobj = Calendar.getInstance();
 System.out.println(df.format(calobj.getTime()));
Tresatrescha answered 8/1, 2015 at 10:20 Comment(4)
i know how to get time.....but iam not getting how to put time in same bubble like in whatsappChalcidice
you have to make custom adapter and use custom listview for thatTresatrescha
Done it..but i cant add time stamp in chat bubble...can you tell me how to do this.. i have followed up this project androidhive.info/2014/10/…Chalcidice
1. add view in the xml where you want the time 2. get a reference to it in the java class related to the activity 3. parse a String according to the answer above 4. attach this string from #3 to the view you have a reference forUniform
C
0

A bit late but maybe someone still needs it. Please consider that the in the following scenario I set the gravity and the textAlignment programmatically, depending on the position of the bubble, left or right. This is how I managed to do this:

Normal View

Normal View

Layout boundaries enabled

Layout boundaries enabled

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/chat_wrapper"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

  <RelativeLayout
      android:id="@+id/message_wrapper"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dip">

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="100dip"
        android:paddingBottom="5dip"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/message_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/message_text"
        android:layout_below="@+id/message_text"
        android:textColor="#000000"
        android:textSize="9sp"/>

  </RelativeLayout>

</LinearLayout>

Other info

The background is a 9-patch image that I set programmatically, can be found with a simple search. Tested on:

  • Samsung Galaxy S4
  • Android 22.0.1
Claustral answered 3/7, 2015 at 16:57 Comment(0)
B
0

I have downloaded the code from the androidhive link you have to change following two layout files

list_item_message_right.xml. 
list_item_message_left.xml.

add a new textview for timestamp in above layout files and then set their values time stamp values in MessageListAdapter class

    lblFrom.setText(m.getFromName());

//set time stamp here
Bontebok answered 3/7, 2015 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.