tags:

views:

67

answers:

2

I tried with threads, but android throws "CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.".

So how can I wait 3 seconds and then hide the view, letting the GUI responsive?

--

A Timer uses another thread either, so it will not solve..

A: 

You can use runOnUiThread method to access UI thread.

Aleksander O
The GUI becomes non responsive if I put it to wait the 3 secs..
Tom Brito
Brandon already posted a sample of code which I meant. You should call that method only from non-UI thread.
Aleksander O
Actually, its not needed to run inside another thread. Just using runOnUiThread and wait() instead of sleep() works without make the GUI unresponsive.. :)
Tom Brito
Ops, no, its still not working.. It was throwing an exception, and my Logcat was showing nothing.. I needed to restart for logcat back to work.. :/
Tom Brito
+3  A: 

Spawn a separate thread that sleeps for 3 seconds then call runOnUiThread to hide the view.

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Do some stuff
            }
        });
    }
};
Brandon
A `Message` and a `Handler` would be a more Android-y way to do the same thing, particularly when it comes to UI manipulation.
Dave
@Brandon it still makes the GUI unresponsible to my key press while the thread is sleeping.
Tom Brito
@Dave I would like to read more about..
Tom Brito
@Brandon oh, sorry, I see now that the spleep is outside the runOnUiThread.. But this won't work for me, becouse I will in this same method make the View visible, then sleep, and make it invisible. Got it? Like a blink. And, by the way, I can't use Toasts becouse I can show just 1 at a time.
Tom Brito
Oh god.. what a travel.. it worked! =D just put the setVisible before the first thread starts.. XD
Tom Brito