BamsBamx
BamsBamx

Reputation: 4256

Created clock on Android is not updating

I´m trying to create a fullscreen clock. I managed to set text for hours, minutes and seconds. Well, but when I start the app, it shows the time but the UI is not updating... I dont know how to do it, I read this tutorial but i dont understand it... any one can explain me how to consantly update the UI?

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

final TextView txtHour = (TextView)findViewById(R.id.TxtHour);
final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute);
final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds);
final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds);

 final Integer hora = new Integer(Calendar.HOUR_OF_DAY);
 final Integer minutos = new Integer(Calendar.MINUTE);
 final Integer segundos = new Integer(Calendar.SECOND);
 final Long milisegundos = new Long (System.currentTimeMillis());
    timer = new Timer("DigitalClock");
    Calendar calendar = Calendar.getInstance();
    // Get the Current Time
    final Runnable updateTask = new Runnable() {
        public void run() {
/** txtHour.setText(hora.toString());
 txtMinutes.setText(minutos.toString());
 txtSeconds.setText(segundos.toString()); */
 txtMilliseconds.setText(milisegundos.toString());
 Toast toast1 = Toast.makeText(getApplicationContext(), milisegundos.toString(), Toast.LENGTH_SHORT);
toast1.show();
        }
    };

    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(updateTask);
        }
    }, 1, 1000);

}

}

Just tell me how to complete it to update UI,please...

Upvotes: 0

Views: 1415

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

see this example for Create a Apps to Show Digital Time in Android .And in your case use

runOnUiThread for Upadting time on UI.as

CurrentActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //UPDATE TIME HERE
    }
});

and your code look like:

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

    final TextView txtHour = (TextView)findViewById(R.id.TxtHour);
    final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute);
    final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds);
    final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds);


        timer = new Timer("DigitalClock");
        Calendar calendar = Calendar.getInstance();
        // Get the Current Time
        final Runnable updateTask = new Runnable() {
            public void run() {
     final Integer hora = new Integer(Calendar.HOUR_OF_DAY);
     final Integer minutos = new Integer(Calendar.MINUTE);
     final Integer segundos = new Integer(Calendar.SECOND);
     final Integer milisegundos = new Integer(Calendar.MILLISECOND);
     txtHour.setText(hora.toString());
     txtMinutes.setText(minutos.toString());
     txtSeconds.setText(segundos.toString());
     txtMilliseconds.setText(milisegundos.toString());
            }
        };

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(updateTask);
            }
        }, 1, 1000);
 }

}

Upvotes: 0

Mike
Mike

Reputation: 2136

You didn't mention which tutorial you're working on, so just in case, you'll probably want to use AsyncTask.

Upvotes: 1

Related Questions