-2

I just recently started playing around with Java/Android.

I am trying to make a very simple Android app. I want a number to be progressively increased. Something like:

int x = 0;

then every 0.1 seconds, x++. Then I can set the text of a textview

.setText(String.valueOf(x));

So in the program it will have an integer number increasing from 0 by 1 every 0.1 seconds.

Meanwhile all other functions/code should run normally while this is happening in the background

Friedpanseller
  • 654
  • 3
  • 16
  • 31

2 Answers2

3

you could use the TextView's internal handler and its postDelayed method, to increment the int . E.g.

textView.postDelayed(new Runnable() {
       @Override
       public void run() {
            textView.setText(String.valueOf(++x));    
            textView.postDelayed(this, 100);
       }
  }, 100);

where 100 is 100 milliseconds. Don't forget to call

textView.removeCallbacks(null); 

when your activity is paused

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

you can use:

int x = 0;

TimerTask scanTask;
Timer t = new Timer();

public void incrementValue(){

scanTask = new TimerTask() {
        public void run() {
               x++;
        }};

    t.schedule(scanTask, 100, 100); 
 }

and use t.cancel(); whenever you want to stop the task.

chetna
  • 94
  • 5