I am trying to make an app for students, and in the home screen of the app I would like to display a message like "Hey! Good Morning" and it should change to "Hey!Good evening" when it is after 15:00hrs of a day and so on. How can I achieve this ?
Asked
Active
Viewed 255 times
0
-
Use system, i.e mobile, time. see this http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android – Mehraj Malik Jul 29 '16 at 12:11
3 Answers
0
you can get the time or hour by
import java.util.Calendar
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR);
from this you can choose a String for your textview:
TextView mytextview=(TextView) findViewByID(...);
String message="";
if(hours<11){
message="good morning"
}else if(hours>=11 & hours<17){
message=...}
mytextview.setText(message);

Stephan Huewe
- 124
- 1
- 7
0
Please check this one.It may help you.
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
if(timeOfDay >= 0 && timeOfDay < 12){
Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 12 && timeOfDay < 16){
Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 16 && timeOfDay < 21){
Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 21 && timeOfDay < 24){
Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}

Mehraj Malik
- 14,872
- 15
- 58
- 85
0
You can place a check for time of day in your activity in onResume()
. This will check whenever the screen which has to display the text comes to foreground. So, even when you return to it from a different screen, the check will be made. For realtime check you will need a mechanism for absolute callback of the event [of 1500 hrs].
This might be a bit cumbersome and we need more info to decide on a mechanism. You should also decide if this is exactly what you want to do or not.
Not showing checking code because of its trivial nature.

Shaishav
- 5,282
- 2
- 22
- 41