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

ε祈祈猫儿з 提交于 2019-12-09 23:22:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!