24

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..

1
  • Simple example for Splash screen in Android, userful to you:here
    – Popeye
    Nov 20, 2012 at 9:19

7 Answers 7

72

There is an easier way to do it: use View.postDelayed(runnable, delay)

View view = yourView;
view.postDelayed(new Runnable() {
        public void run() {
            view.setVisibility(View.GONE);
        }
    }, 3000);

It's not very precise: may be hidden in 3.5 or 3.2 seconds, because it posts into the ui thread's message queue.

Use post() or runOnUiThread() just something as setTimeout().

3
  • 4
    I suppose it should be postDelayed and not post
    – marcinj
    Jan 30, 2013 at 14:26
  • 1
    +1 As its far less trouble making than using wait or sleep. But you have a typo (its Runnable() { ... }, and there's a ; missing) please fix it so you may get more upvotes. Also points for mentioning runOnUithread. And what comes next is because i'm kinda obsessive with details, but perhaps correct the english use in some sentences? (Don't kill me please! ;) ). I'm just saying because I think this should be the accepted answer.
    – acrespo
    Oct 9, 2013 at 17:05
  • The syntactically correct version is: view.postDelayed(new Runnable() { public void run() { view.setVisibility(View.GONE); } }, 3000); May 3, 2014 at 18:05
22

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
                }
            });
        }
    };
    thread.start(); //start the thread
5
  • 3
    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
    Jul 14, 2010 at 17:39
  • it still makes the GUI unresponsible to my key press while the thread is sleeping. Jul 14, 2010 at 18:06
  • 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. Jul 14, 2010 at 18:09
  • Oh god.. what a travel.. it worked! =D just put the setVisible before the first thread starts.. XD Jul 14, 2010 at 18:15
  • SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread. Apr 7, 2016 at 20:50
20

Without the need to have a reference to a view or sleep a thread:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // do stuff
        }
    }, 3000);
0
7

I know this is a stretch, but here's an answer with coroutines if you happen to use them:

    lifecycleScope.launch {
        delay(3000)
        header.visibility = View.GONE
    }
2

You can show your view and then hide it this way.

View view = yourView;
view.setVisibility(View.VISIBLE);
new Handler().postDelayed(() ->  view.setVisibility(View.GONE), 3000);
2
  • Lovely man. I just wanted to dismiss a fragment and this worked out well. new Handler().postDelayed(() -> mWarningFragment.dismiss(), 3000);
    – saumilsdk
    Jul 2, 2021 at 8:09
  • Glad, that helped. Please upvote if this solved your issued Jul 7, 2021 at 12:19
1

Latest Kotlin code:

YourView.visibility = View.VISIBLE
// removing the banner after 3 sec
YourView.postDelayed({
   YourView.visibility = View.GONE
},3000)
-1

I got Stuck in Such Kind of Problem. So, I heard about Coroutines. That they can do these kind of tasks without disturbing Main Thread.

lifeCycleScope.launch{ delay(2000) binding.imageView.visibility = View.GONE }

1
  • How is your answer, different from this answer? Unnecessarily creating duplicates. Jul 2, 2022 at 3:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.