-1

I want to create TextView that will automatically update texts in the Activity with realtime. Because I want it to run offline, I will populate the text to be changed with real dates from string-array.

Example of what I'm aiming to build. Today is Saturday, 27-06-2020. "Reading of the day" etc.

Please how do I achieve this. If the process of creating this requires other use of Library order than TextView please teach me. Thanks !

yusufX019
  • 58
  • 5
Joseph
  • 400
  • 4
  • 19
  • Look here how to get current date, then display it in textview after formatting. You can do this in Activity onResume callback https://stackoverflow.com/questions/5369682/how-to-get-current-time-and-date-in-android – OMIsie11 Jun 27 '20 at 12:09

2 Answers2

1

What you need is to get the current local date and the day. You can get the current date by:

LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String currDate = localDate.format(formatter);

and you can get the current day by:

DayOfWeek dayOfWeek = DayOfWeek.from(LocalDate.now());

Now your result string would be:

String res = "Today is " + dayOfWeek + ", " + currDate + ". \"Reading of the day\""; 

Now to update the TextView you created before, let say;

TextView tv = findViewById(R.id.tv_id);
tv.setText(res);
Usama Ahmed
  • 66
  • 11
  • Thanks for answering. I want the text to change as well. This wonderful code you gave me shows same text with real time. I want it as the time is changing, the text will equally change to a new text. – Joseph Jun 27 '20 at 12:39
  • Each new day should invoke the text for the day stored in String.xml – Joseph Jun 27 '20 at 12:46
  • It will work when the day changes. Now if you want to get the text from strings.xml, check the below link and apply it to your scenario accordingly and if my answer is good to go for you please mark it as answered. https://stackoverflow.com/questions/2183962/how-to-read-value-from-string-xml-in-android – Usama Ahmed Jun 28 '20 at 13:06
  • Thanks for the link. I hope it would be able to update some texts in Second day reading text by tomorrow. If it does not, i hope you will still give me some help. – Joseph Jun 28 '20 at 20:10
0

You can use the Calendar class in java.util package.

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Samudra Ganguly
  • 637
  • 4
  • 24
  • what about the "reading of the day..." auto update with LocalDate? – Joseph Jun 27 '20 at 14:30
  • What do you mean by "reading of the day..."? Please elaborate. – Samudra Ganguly Jun 27 '20 at 15:05
  • I am giving instance of TextView to update with LocalDate everyday for intace "reading of the day for day 01 of the week..." , "reading of the day for day 02 of the week..." etc – Joseph Jun 27 '20 at 16:09
  • I am sorry but I don't know what is meant by "Reading of the day". Please explain. – Samudra Ganguly Jun 27 '20 at 16:13
  • Do you mean the day of the week like Saturday Sunday etc.? – Samudra Ganguly Jun 27 '20 at 16:15
  • No. The LocalDate shows that. What I mean is for instance Today is Saturday, 27-06-2020 Today's message is love your neighbor as yourself. – Joseph Jun 27 '20 at 16:21
  • Then by tomorrow it will change to Today is Sunday, 28-06-2020 Today's message is trust in God who is the creator of universe. After "Today's message is" + "another instance of text" . The second text in quotes is what I want to continually auto update with respect to LocalDate – Joseph Jun 27 '20 at 16:27
  • Then use LocalDate reference. I don't know about this. Is it some kind of religious thing? – Samudra Ganguly Jun 27 '20 at 16:43
  • If LocalDate reference will serve the purpose, how do I use it? – Joseph Jun 27 '20 at 17:24
  • Or Is there a way to code some custom texts in the calendar so that whenever I call the string result it will update time, date and custom texts? – Joseph Jun 27 '20 at 17:32
  • I need to set custom texts to update with LocalDate for 365days of the year – Joseph Jun 27 '20 at 23:26