Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a message along with the time in groupchat?

Tags:

java

android

I am developing an android application in that, I have an group chat functionality when i am send a message on group chat i need to display message along with the time(at the end of the message the time need to display on every chat message).

Here is my layout code

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

     <TextView
        android:id="@+id/txtInfo"
        android:layout_width="wrap_content"
        android:layout_height="30sp"
        android:layout_gravity="left"
        android:textSize="12sp"
        android:textColor="@color/lightred"
        android:textStyle="italic"
        />

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:layout_marginRight="80dp"
        android:background="@color/grey"
        android:textColor="@color/white"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textStyle="italic|bold"
        />

</LinearLayout>

"@+id/txtInfo" used for get the current date and time & "@+id/txtMsg" used for display the message.

now my output is looking like this enter image description here

but i am expecting my output as, enter image description here

my programming code is below,

 public class MessagesListAdapter extends BaseAdapter {

        private Context context;
        private List<ChatMessageObjects> messagesItems;

        public MessagesListAdapter(Context context, List<ChatMessageObjects> navDrawerItems) {
            this.context = context;
            this.messagesItems = navDrawerItems;
        }

        @Override
        public int getCount() {
            return messagesItems.size();
        }

        @Override
        public Object getItem(int position) {
            return messagesItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            /**
             * The following list not implemented reusable list items as list items
             * are showing incorrect data Add the solution if you have one
             * */

            ChatMessageObjects m = messagesItems.get(position);

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            if (messagesItems.get(position).getMessage_type() == Constants.IS_TYPE_CHAT_IMAGE) {
                convertView = mInflater.inflate(R.layout.chat_image,
                        null);

                ImageView imageView = (ImageView) convertView.findViewById(R.id.imgView);
                TextView imageLabel = (TextView) convertView.findViewById(R.id.lblImage);
                if (messagesItems.get(position).isSelf() == 0) {
                    Log.i(Constants.TAG, " the value is from others");
                    try {
                        URL url = new URL(messagesItems.get(position).getMessage());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        imageView.setImageBitmap(BitmapFactory.decodeStream(input));
                    } catch (IOException e) {
                        Log.i(Constants.TAG, e.toString());
                    }
                } else if (messagesItems.get(position).isSelf() == 1) {
                    Log.i(Constants.TAG, " the value is itself");
                    imageView.setImageURI(Uri.fromFile(imgFile));
                    imageLabel.setText(messagesItems.get(position).getFromName());
                }
            } else if (messagesItems.get(position).getMessage_type() == Constants.MESSAGE_TYPE_MSG) {
                if (messagesItems.get(position).isSelf() == 1) {
                    // message belongs to you, so load the right aligned layout
                    convertView = mInflater.inflate(R.layout.chat_message_right,
                            null);
                    TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
                    //date and time added here
                    TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
                    txtMsg.setText(m.getMessage());
                    lblFrom.setText(m.getFromName());
                    date.setText(m.getDate());
                } else if (messagesItems.get(position).isSelf() == 0) {
                    // message belongs to other person, load the left aligned layout
                    convertView = mInflater.inflate(R.layout.chat_message_left,
                            null);
                    TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
                    //date and time added here
                    TextView date = (TextView) convertView.findViewById(R.id.txtInfo);
                    txtMsg.setText(m.getMessage());
                    lblFrom.setText(m.getFromName());
                    date.setText(m.getDate());
                }

            }

            return convertView;
        }

date.setText(m.getDate()); code get my date in this format"28-aug-2015 11:46:11pm". I need to convert my date in this format"11:46 pm" how can i change this format and how to display chat message along with time as looking above please help me.

like image 847
selva simon p Avatar asked Oct 31 '22 20:10

selva simon p


2 Answers

Simply change your LinearLayout orientation from "vertical" to "horizontal".

You can use Time to represent hours and minutes, like this:

    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();
    String timeInput = today.hour + ":" + today.minute;

Better approach is to use SimpleDateFormat:

    SimpleDateFormat s = new SimpleDateFormat("HH:mm a");
    String msgTime = s.format(new Date());

EDIT:

Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String msgTime = today.format("HH:mm a")

Use this example to show formatted Time.

like image 149
Ivan V Avatar answered Nov 15 '22 06:11

Ivan V


Formmating the date can be done like this:
Declare a SimpleDateFormat object with the desired format -

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");. You can set the format string as explained here.
Then you can get a string to hold your time - String myTime = sdf.format(new Date()); and add that string to your message.

EDIT -
For time format of 12:30 am/pm you should use SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");.

like image 33
TDG Avatar answered Nov 15 '22 05:11

TDG