Tom Brito Tom Brito - 1 month ago 14
Android Question

How to show a view for 3 seconds, and then hide it?

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

Answer Source

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