问题
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.
回答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()));
回答2:
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

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
回答3:
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
来源:https://stackoverflow.com/questions/27837692/how-to-create-a-chat-activity-with-the-time-stamp-in-bubble