saalgu
saalgu

Reputation: 1

How to show a button after 5 seconds in android studio?

I have an app that shows a disclaimer at the beginning of the program. I want a button to remain invisible for 5 seconds, and then become visible. I set up a thread that sleeps for 5 seconds, and then tries to make the button visible. However, I get this error when I execute my code:

08-02 21:34:07.868: ERROR/AndroidRuntime(1401): 

How can I count 5 seconds, and then make the button visible?

Upvotes: 0

Views: 2538

Answers (2)

MarcGV
MarcGV

Reputation: 1262

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //your code
                }
            } 
        }
    }, 5000);

Upvotes: 1

Drilon Blakqori
Drilon Blakqori

Reputation: 2826

yourButton.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!getActivity().isFinishing()) { // make checks to see if activity is still available
                yourButton.setVisibility(View.VISIBLE);
            }
        }
    }, 1000 * 5);    // 5 seconds

Upvotes: 3

Related Questions