Bert B. Bert B. - 1 year ago 60
Java Question

Setting an Android Button visible after a certain period of time?

I have a button that I don't want to be clickable until a certain amount of time has run (say, 5 seconds?) I tried creating a thread like this

continueButtonThread = new Thread()
public void run()
try {
catch(InterruptedException ex){



But I can't modify the setVisibility property of the button within a different thread. This is the error from the LogCat:

10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Any other way to get around this?

Answer Source

The problem is that you can touch views of your activity only in UI thread. you can do it by using runOnUiThread function. I would like to suggest you to use

handler.postDelayed(runnable, 5000)`