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

Viewed 36625

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

6 Answers

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
    }

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);

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 }

Related