0

i am developing an android app where in i am displaying the date and time for the user on the main screen of my app. I am using the below code for it.

        SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy");
        String strtDates = sdf.format(cal.getTime());
        textviewfordate.setText(strtDates);

        clk = (DigitalClock)findViewById(R.id.digitalClock);

However, when the time is changed, the date doesnot get updated without refreshing the screen. For example when the time is 11:59:00 PM and when the time changes to 12:00:00 AM, the date will not be updated. It gets updated only when the screen is refreshed. I want the date to be changed automatically without refreshing the screen.

Not getting how to do it! Please Help! Thanks!

sanjana
  • 641
  • 2
  • 15
  • 36

1 Answers1

0

If you set a text to textview in onCreate(), it will run only once when that method is called and won't get updated everytime the time changes.

For your need, you have to use handler or executor or any other thread which runs for every user defined time, and you have to set the current time text to textview inside them. So, that the text will be updated.

And also refer the following links, they will help you:

Android run thread in service every X seconds

How to execute a function every two seconds in Java on Android?

display data after every 10 seconds in Android

Community
  • 1
  • 1
Hariharan
  • 24,741
  • 6
  • 50
  • 54