Note that there are some explanatory texts on larger screens.

plurals
  1. POChange text color in button every interval of time
    primarykey
    data
    text
    <pre><code>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); </code></pre> <p>in Logcat:</p> <pre><code>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 </code></pre> <p>which means that <code>btn1.setTextColor(...)</code> is executing <strong>only once</strong>! I would like the <strong>Button Text</strong> to be changed every <strong>1 second</strong>.</p> <p>Any expert can help?</p> <p>Thank to Ole I could find out a solution for my problem that I would like to share with you:</p> <p><strong>SOLUTION:</strong></p> <pre><code>// 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!! </code></pre> <p>1)Do not forget to declare <code>Handler hd</code><br> 2)Also, <code>hd = new Handler()</code> in <code>onCreate()</code><br> 3) Use <code>startRepeatingTask()</code> wherever you desire your repetitive code to be repeated.<br> 4) Use <code>stopRepeatingTask()</code> wherever you desire to stop repeating.<br></p> <p>Cheers! ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload