2

I am new android and I would appreciate some help. I have this code:

        dateatm = (TextView) findViewById(R.id.date);
        timeatm = (TextView) findViewById(R.id.tvTime);
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
        SimpleDateFormat värk = new SimpleDateFormat("HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        String timeNow = värk.format(currentDate.getTime());
        dateatm.setText(dateNow);
        timeatm.setText(timeNow);

As you can see, I am getting date and time out of it. What I want tho is that there would be 1 second loop, so after every 1 second, it will update date and time again and again to the newer one to get basically clock. I am pretty newcomer so all help is appreciated how to get this done. If you would bring exact examples I would appreciate as it makes it more easier to understand, not just "Oh, use that and that and that. Thank you!".

Thanks for your help and time, Elven :)


Error for answer 1:

08-10 18:55:41.335: ERROR/AndroidRuntime(886): FATAL EXCEPTION: Timer-0
08-10 18:55:41.335: ERROR/AndroidRuntime(886): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.view.View.invalidate(View.java:5279)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.widget.TextView.checkForRelayout(TextView.java:5507)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.widget.TextView.setText(TextView.java:2724)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.widget.TextView.setText(TextView.java:2592)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at android.widget.TextView.setText(TextView.java:2567)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at viimane.voimalus.MainStuff$1.run(MainStuff.java:55)
08-10 18:55:41.335: ERROR/AndroidRuntime(886):     at java.util.Timer$TimerImpl.run(Timer.java:284)

1 Answer 1

5

I just solved this issue in my project (executing code at specific interval) :)

Edited to reflect updated answer:

protected static final int REFRESH = 0;
private Timer timer;
private TimerTask refresher;

private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case REFRESH:
            /* your code here */
            break;
        default:
            break;
        }
    }
};

// Initialization code in onCreate or similar:

timer = new Timer();    
refresher = new TimerTask() {
    public void run() {
        ActivityMain.this.sendEmptyMessage(REFRESH);
    };
};
// first event immediately,  following after 1 seconds each
timer.scheduleAtFixedRate(refresher, 0, 1000); 
4
  • hmm, it gives me error after 1 second. I'll put error into opening post. Do you know what might be cause?
    – Elven
    Aug 10, 2011 at 18:56
  • 1
    You have to update UI from UI thread. TaskTimer's code appears to in different. We have to send signal from TaskTimer to Activity that it's time to refresh data. ActivityMain.this.sendEmptyMessage(REFRESH); sends message, Handler receives. Then you check what message you received and if it is REFRESH - execute code you wrote in your question.
    – Im0rtality
    Aug 10, 2011 at 20:46
  • But where goes what exactly? Do I change ActivityMain to that class on what it is? Cuz it gives me error: It cannot be resolved to type :/
    – Elven
    Aug 10, 2011 at 21:27
  • It's code taken from my project, where ActivityMain is class of activity which does the updates.
    – Im0rtality
    Aug 11, 2011 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.