Android run method/code after UI shows

I want to load the UI, then once the user can see the UI start some other code. I want to run that code on the UI thread.

 onCreate(){ setContentView(R.layout.mylayout);//myLayout starts with a blue background myLayout = (LinearLayout).... myLayout.setBackgroundColor(red); } onViewDisplayed(){ //myLayout is visible and shows a red background while the UI thread is blocked by Thread.sleep() Thread.sleep(5000); myLayout.setBackgroundColor(yellow); }

One way I can get it to work is by doing this:

onCreate(){ setContentView(R.layout.mylayout);//myLayout starts with a blue background myLayout = (LinearLayout).... myLayout.setBackgroundColor(red); handler.postDelayed(runnable,1000);//runnable calls onViewDisplayed() above }

This way isn’t what I want, though. Because if I change postDelayed to 100 ms, then myLayout does not show the red background before Thread.sleep(5000), so all I get to see is the yellow background.

How can I do this with a guaranteed execution AFTER the layout displays without having to specify any delay, if it is possible?