1. Download our Official Android App: Forums for Android!

Apps TextView, setText() not working with Timer

Discussion in 'Android Development' started by Andrew McCandless, Mar 8, 2010.

  1. Andrew McCandless

    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    7
    Joined:
    Mar 8, 2010

    Mar 8, 2010
    7
    0
    25
    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 (Text):
    1. public class HelloAndroid extends Activity {
    2.      /** Called when the activity is first created. */
    3.      @Override
    4.      public void onCreate(Bundle savedInstanceState) {
    5.          super.onCreate(savedInstanceState);
    6.          setContentView(R.layout.main);
    7.          TextView tv = (TextView) this.findViewById(R.id.text);
    8.          
    9.          Timer t = new Timer();
    10.          t.scheduleAtFixedRate(new TestTask(tv), 0, 1000);
    11.      }
    12.  
    13.      class TestTask extends TimerTask{
    14.          TextView t;
    15.          public TestTask(TextView t){
    16.              this.t = t;
    17.          }
    18.          public void run(){
    19.              System.out.println("time = " + System.currentTimeMillis());
    20.              this.t.setText("time = " + System.currentTimeMillis());
    21.          }
    22.      }
    23.  }
     

    Advertisement

  2. markb

    markb Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    148
    Joined:
    Sep 14, 2009

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
  3. jon21569

    jon21569 Newbie
    Rank:
    None
    Points:
    16
    Posts:
    23
    Joined:
    Aug 10, 2008

    Aug 10, 2008
    23
    7
    16
    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:

  4. Andrew McCandless

    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    7
    Joined:
    Mar 8, 2010

    Mar 8, 2010
    7
    0
    25
    Oh wow,

    Thanks a bunch Jon! This is really helpful :)
     
  5. slang

    slang Lurker
    Rank:
    None
    Points:
    25
    Posts:
    2
    Joined:
    May 20, 2011

    May 20, 2011
    2
    0
    25
    i got an error on

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

    here id is underlined how can i fix it
     
  6. noobie_2012

    noobie_2012 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Oct 28, 2012

    Oct 28, 2012
    1
    0
    5
    awesome!
    Thanks :)
     

Share This Page

Loading...