Ok; this is a solution that will give you an idea to build upon. It will solve your date issues and gives you a hint on how to solve how you display your times. This kind of program involves a lot of work to make it viable as an app. You need to take into account timezones, where you store the messages and so on.
Now stack overflow does not serve the purpose of designing programs for people, but to try and help steer people into the right direction.
For the purposes of answering your question; I am covering the basics, I'm not giving you the code ready to run, you will need to do some of the work :)
This could be solved in numerous ways I have chosen the following:
Using shared preferences to store the latest date time. I considered accessing the chat message arrays, but figured this may be simpler. I chose shared preferences so that the date of the last message would be retained when the app is closed and reopened. (Eventually the chat messages would need to be retained in a database [SQLite] so that the messages will still be there when the app is closed and opened). Please see my comment at the end.
public class ChatActivity extends ActionBarActivity {
public static final String MyPREF = "MyPrefs" ;
public static final String DATE_KEY = "DateKey" ;
SharedPreferences prefs;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
initControls();
pref = getSharedPreferences(MyPREF, MODE_PRIVATE);
// Check that the shared preferences has a value
if(contains(DATE_KEY)){
editor = prefs.edit();
// get the current datetime.
editor.Long(DATE_KEY, date);
editor.commit();
// set the text of the textview android:id="@+id/date"
// with the current formatted date.
}
}
In your onclick listener, you test that the last stored date is todays date, if it's not, it is converted into a chatmessage, so it's value can be added to the array and displayed. You will have to tweak how you format these message types, and you can even add a unique id for these date messages and test for that, or test for no id.. there's many solutions.
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/.../
}
ChatMessage chatMessage = new ChatMessage();
long LastDate = pref.getLong(DATE_KEY, date);
// create an empty chat message with only a date.
// check if the last used date is not today's date.
// if it's not today's date, save it and display it as
// an archived bubble.
if(!LastDate.isToday()){
ChatMessage dateholder = new ChatMessage();
// You'll need to format the date
dateholder.setDate(Formatted LastDate);
// set/change the text of the textview android:id="@+id/date"
// with the current formatted date.
}
// put the current date time into your preferences.
editor = prefs.edit();
editor.putString(DATE_KEY, date);
editor.commit();
/.../
// this is now setting up the new chat message.
chatMessage.setDate(DateFormat.getDateTimeInstance().format(new Date()));
/.../
displayMessage(chatMessage);
}
});
xml Ok, now you don't want the date floating at the top of the screen when there's only one message. So group it with the scroll list and then set this relative to the screen layout as the list alone was. A linear layout will do for this, just ensure it's orientation is set to vertical.
Pin these together.
<LinearLayout
android:layout_above="@+id/messageEdit"
android:layout_below="@+id/meLbl"
android:id="@+id/layout"
.../orentation:vertical ../
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
/..//>
<ListView
android:id="@+id/messagesContainer"
.../
...//>
</LinearLayout>
<TextView
android:id="@+id/meLbl"
/.../
/>
<TextView
android:id="@+id/friendLabel"
/.../
/>
Change display for each message You will need to stop displaying the date time for each message. If you wish to show the time for each message individually, and skip those within the same minute, as you mentioned in the comments, then I've given you the framework, to work out how to test for this and the program logic of whether or not it is displayed.
You need to change this in your:
public View getView(final int position, View convertView, ViewGroup parent) {
holder.txtMessage.setText(chatMessage.getMessage());
// TODO,
// do a check if(mins are the same don't post
// else post the format without the date)
// holder.txtInfo.setText(chatMessage.getDate());
Don't forget to apply all necessary imports.
I suggest if you start to use databases that you access the last date from there without shared preferences, or continue to use shared preferences. This becomes more complicated as there are multiple chats being saved, in which case using the database would be preferable.