0
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    // TODO Auto-generated method stub
    Log.i("first iteration","first iteration");
    btn1.setTextColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255))); 
    Log.i("iterating","iteratinggggggggg"); 
                        }
                    }, 0, 1000);

in Logcat:

01-07 02:39:09.789: I/first iteration(16568): first iteration
01-07 02:39:09.789: I/iterating(16568): iteratinggggggggg
01-07 02:39:10.781: I/first iteration(16568): first iteration

which means that btn1.setTextColor(...) is executing only once! I would like the Button Text to be changed every 1 second.

Any expert can help?

Thank to Ole I could find out a solution for my problem that I would like to share with you:

SOLUTION:

// UPDATING BTN TEXT DYNAMICALLY
    Runnable myRunnableUpdater = new Runnable()
    { 
        public void run() {
            colorGenerator();
            hd.postDelayed(myRunnableUpdater, 1000);
        }
    };
    void startRepeatingTask()
    {
        myRunnableUpdater.run(); 
    }
    void stopRepeatingTask()
    {
        hd.removeCallbacks(myRunnableUpdater);
    }

    private void colorGenerator() {
        btn1.setTextColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
    }
    //END OF UPDATING BTN TEXT DYNAMICALLY!!

1)Do not forget to declare Handler hd
2)Also, hd = new Handler() in onCreate()
3) Use startRepeatingTask() wherever you desire your repetitive code to be repeated.
4) Use stopRepeatingTask() wherever you desire to stop repeating.

Cheers! ;)

Alex
  • 1,639
  • 3
  • 18
  • 33
  • Are you sure? You have 2 "first iteration" Log outputs 1 sec apart and it will always output "first iteration" when the TimerTask repeats as it's a String. – A--C Jan 07 '13 at 00:50
  • yes i'm sure, i tested it many times! maybe because rgb is a static method and cannot be changed every instance? – Alex Jan 07 '13 at 00:51
  • And you are not getting any errors in LogCat? You are updating the UI from outside the main thread. I would expect your application to force close. – Ole Jan 07 '13 at 00:57
  • yes it is force closing true i forgot to mention that.. – Alex Jan 07 '13 at 00:58
  • That would be why everything isn't being updated :) – A--C Jan 07 '13 at 00:58
  • No it's not repeating, the color changes only once, then the application closes! – Alex Jan 07 '13 at 00:58
  • I don't understand guys where's the problem? :P – Alex Jan 07 '13 at 00:59
  • You cannot change/update your view (button) outside of the UI thread. – wtsang02 Jan 07 '13 at 01:00
  • @wtsang02 thank you for clarifying, you mean i should use the runOnUiThread thingy – Alex Jan 07 '13 at 01:02

1 Answers1

1

Your application is force-closing because you are trying to update UI elements from a thread created by the Timer. Only the main thread is allowed to update the UI. This can be solved using a Handler

Have a look at this answer on how to implement this.

Community
  • 1
  • 1
Ole
  • 7,899
  • 2
  • 29
  • 34