0

I want to make an ImageButton disappear after being clicked, and after two seconds the ImageButton appear again.

I've searched on the stackoverflow but I cannot find any answer.

I tried to use Timer, but my activity start to crash.

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View button) {
                button.setBackgroundResource(R.drawable.avatar_dead);
                final long changeTime = 1000L;
                button.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        button.setBackgroundResource(R.drawable.avatar_small);
                    }
                }, changeTime);
            }
        });

1 Answers1

0

You can use postDelayed method, the first argument is a Runnable and the second are milliseconds to wait before execute it :

new Handler().postDelayed(()->myButton.setVisibility(View.VISIBLE),2000);
ollaw
  • 2,086
  • 1
  • 20
  • 33
  • 1
    Handler is not abstract, maybe you've imported something wrong. https://developer.android.com/reference/android/os/Handler.html – ollaw May 14 '17 at 22:22