views:

7216

answers:

5

I want to make a pause between two lines of code, Let me explain a bit :

-> the user clicks a button (a card in fact) and I show it by changing the background of this button :

thisbutton.setBackgroundResource(R.drawable.icon);

-> after let's say 1 second, I need to go back to the previous stade of the button by changing back its background :

thisbutton.setBackgroundResource(R.drawable.defaultcard);

-> I've tried to pause the thread between these two lines of code with :

try {Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

but It does not work. Maybe it's the process and not the Thread that I need to pause ??

What is the best solution to implement this ?

Many txs for your help.

H.

I've tried (but it doesn't work ?)

new Reminder(5);

with :

public class Reminder {

Timer timer;

        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }

        class RemindTask extends TimerTask {
            public void run() {
                System.out.format("Time's up!%n");
                timer.cancel(); //Terminate the timer thread
            }
        }  
    }
+2  A: 

You probably don't want to do it that way. By putting an explicit sleep() in your button-clicked event handler, you would actually lock up the whole UI for a second. One alternative is to use some sort of single-shot Timer. Create a TimerTask to change the background color back to the default color, and schedule it on the Timer.

Another possibility is to use a Handler. There's a tutorial about somebody who switched from using a Timer to using a Handler.

Incidentally, you can't pause a process. A Java (or Android) process has at least 1 thread, and you can only sleep threads.

Daniel Yankowsky
+2  A: 

In addition to Mr. Yankowsy's answers, you could also use postDelayed(). This is available on any View (e.g., your card) and takes a Runnable and a delay period. It executes the Runnable after that delay.

CommonsWare
A: 

This is what I did at the end of the day - works fine now :

@Override
    public void onClick(View v) {
     my_button.setBackgroundResource(R.drawable.icon);
     // SLEEP 2 SECONDS HERE ...
     final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
                public void run() { 
                        handler.post(new Runnable() { 
                                public void run() { 
                                 my_button.setBackgroundResource(R.drawable.defaultcard); 
                                } 
                        }); 
                } 
        }, 2000); 
    }
Hubert
+3  A: 

You could simplify your code by removing the Timer and using the Handler's postDelayed() method:

@Override
public void onClick(View v) {
    my_button.setBackgroundResource(R.drawable.icon);
    // SLEEP 2 SECONDS HERE ...
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
         public void run() { 
              my_button.setBackgroundResource(R.drawable.defaultcard); 
         } 
    }, 2000); 
}
tronman
A: 

I am having a similiar problem. I am using a post delayed with runnable in a splash screen. When the timer runs out I want to move to a second splash screen. The second splash screen uses the same code as the first. This causes an error when the second screen displays.

Mark