Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Find everything you need for the Galaxy S5 and discuss it in our S5 forum!
Have you seen that OnePlus One's awesome camera?? The forum is over here!

test: Reply
 
LinkBack Thread Tools
Old March 8th, 2010, 03:58 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2010
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default TextView, setText() not working with Timer

Hi all,

I just started working with the Android SDK. I've read up on a bit of the literature at developer.android.com but coudln't wait to get my hands dirty writing some code. Well, dirty they are...

I'm trying to write a simple toy application that updates a string every second with the current time (unformatted). With the code below, my console prints out the current time every second, but the TextView doesn't. The TextView seems to update once at the beginning of the program, but otherwise stays the same. Sometimes I'll click the button and it will change once or twice, but that's about it.

I'm not really sure what's wrong here. The timer seems to be working fine, but not the TextView. I looked for something in the TextView that might allow me to force an instance of the TextView class to update, but could not find anything (which is what I would expect, since setText() should automatically update the TextView).

Anywho, my code is below. I'm using Android 2.1 (emulated). Any help would be greatly appreciated

Code:
public class HelloAndroid extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         TextView tv = (TextView) this.findViewById(R.id.text);
         
         Timer t = new Timer();
         t.scheduleAtFixedRate(new TestTask(tv), 0, 1000);
     }
 
     class TestTask extends TimerTask{
         TextView t;
         public TestTask(TextView t){
             this.t = t;
         }
         public void run(){
             System.out.println("time = " + System.currentTimeMillis());
             this.t.setText("time = " + System.currentTimeMillis());
         }
     }
 }

Andrew McCandless is offline  
Last edited by Andrew McCandless; March 8th, 2010 at 04:01 AM.
Reply With Quote
sponsored links
Old March 9th, 2010, 12:23 PM   #2 (permalink)
Member
 
Join Date: Sep 2009
Location: Birmingham, UK
Posts: 148
 
Device(s): G1 (Developer model), HTC Tattoo, Nexus One.
Carrier: Not Provided

Thanks: 1
Thanked 26 Times in 22 Posts
Default

Hi,

You're only allowed to update GUI elements from the GUI thread.
If you want to update them from another thread, as you're doing, then you need to pass a message back to the GUI thread and get it to do the update for you. You can do that using the Handler class.

There's an example here:

Common Tasks and How to Do Them in Android | Android Developers

Also see, Example ProgressDialog with a second thread, on this page:

Creating Dialogs | Android Developers

I hope that helps.

Mark
markb is offline  
Reply With Quote
Old March 9th, 2010, 12:40 PM   #3 (permalink)
Junior Member
 
Join Date: Aug 2008
Posts: 23
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 8 Times in 7 Posts
Default How to display time

Here is source code and an attached android project showing how to display the time.

package com.displaytime;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class DisplayTime extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

updateTime();

handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}

private Runnable updateTimeTask = new Runnable() {
public void run() {
updateTime();
handler.postDelayed(this, 1000);
}
};

private void updateTime() {
final TextView time = (TextView) findViewById(R.id.time);
final Date currentTime = new Date( System.currentTimeMillis() );
final SimpleDateFormat formatterTime = new SimpleDateFormat( "H:mm:ss" );
time.setText( formatterTime.format(currentTime) );
}

@Override
protected void onStop() {
super.onStop();
handler.removeCallbacks(updateTimeTask);
}

@Override
protected void onResume() {
super.onResume();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}

@Override
protected void onDestroy() {
super.onDestroy();
if ( handler != null )
handler.removeCallbacks(updateTimeTask);
handler = null;
}

private Handler handler = new Handler();
}
Attached Files
File Type: zip DisplayTime.zip (42.8 KB, 183 views)
jon21569 is offline  
Reply With Quote
The Following User Says Thank You to jon21569 For This Useful Post:
Old March 9th, 2010, 05:53 PM   #4 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2010
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh wow,

Thanks a bunch Jon! This is really helpful :)
Andrew McCandless is offline  
Reply With Quote
Old May 21st, 2011, 09:30 AM   #5 (permalink)
New Member
 
Join Date: May 2011
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default i got an error

i got an error on

final TextView time = (TextView) findViewById(R.id.time);

here id is underlined how can i fix it
slang is offline  
Reply With Quote
Old October 28th, 2012, 07:48 AM   #6 (permalink)
New Member
 
Join Date: Oct 2012
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

awesome!
Thanks
noobie_2012 is offline  
Reply With Quote
Reply
Tags
settext, textview, timer


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:29 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.