Home » Android » Showing current time in Android and updating it?

Showing current time in Android and updating it?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to display current time and keep it updating, in this format example: 09:41 Hour:Minute. Is it possible to do in TextView? I tried some ways but I’m not getting what I actually want.

How to&Answers:

Something like this should do the trick

final Handler someHandler = new Handler(getMainLooper());   
        someHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
                someHandler.postDelayed(this, 1000);
            }
        }, 10);

You should keep a reference to the handler and the runnable to cancel this when the Activity goes to pause and resume when it resumes. Make sure you remove all callbacks to handler and set it to null in onDestroy

Answer:

Android has a view for this already.

http://developer.android.com/reference/android/widget/TextClock.html

You can use it directly in XML like so

<TextClock
    android:id="@+id/textClock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

It is min api 17, so you if need to go lower than that just look into

http://developer.android.com/reference/android/widget/DigitalClock.html

Worst case scenario you can subclass textview and steal the code from the textclock source. it should be fairly straightforward

Answer:

  boolean run=true; //set it to false if you want to stop the timer
  Handler mHandler = new Handler();


 public void timer() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (run) {
                    try {
                        Thread.sleep(20000);
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                Calendar c = Calendar.getInstance();
                                int min = c.get(Calendar.MINUTE);
                                int hour=c.get(Calendar.HOUR);
                                TextView.setText(String.valueOf(hour)+":"+String.valueOf(min));
                            }
                        });
                    } catch (Exception e) {
                    }
                }
            }
        }).start();}

in the onCreate call it like this

  timer();

Answer:

Why not use Java’s Timer and TimerTask? They’re very easy, reliable and simple to understand.

Here is an example:

public class ClockCounter extends TimerTask{

    private long time = System.currentTimeMillis();

    @Override
    public void run(){
        time += 1000; //add 1 second to the time
        //convert ms time to viewable time and set MainActivity.text (textview) text to this.
    }

    public long getTime(){ return time; }
}

public class MainActiviy extends Activity{

    public static TextView text;
    private Timer timer;

    @Override
    public void onCreate(){
        //default code
        //set text view according to id
        timer = new Timer();
        timer.schedule(new ClockCounter(), 0, 1000); //schedule clock counter to repeat every 1 seconds (1000 ms)
    }
}

I know there are some notes of what to do in there but any android developer with reasonable experience in Java should be able to figure out how to do those things.

I hope this helped!

EDIT
To format the date to a viewable string, you can use the following code (also using only Java’s shipped APIs)

Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm"); //Hourse:Minutes
String dateFormatted = formatter.format(date); //string representation

Answer:

For getting time and date you can use Calender class

Calendar cal = Calendar.getInstance();
String time = ""+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);