Hadi Aghandeh
Hadi Aghandeh

Reputation: 862

How to delay operation in android(why doesn't handler work?)

I made an animation(I named it slide.xml), my simple app includes a button, when user click the button the animation starts in an ImageView, I want to make the imageView invisible after the animation is finished. I searched internet and I found using handler is what one needs to make this possible.

this is what i tried:

import java.util.logging.Handler;

and for onClick method:

imageViewForGif = (ImageView) findViewById(R.id.imageviewForGif);
    imageViewForGif.setBackgroundResource(R.drawable.slide);
    AnimationDrawable frameAnimation = (AnimationDrawable) imageViewForGif.getBackground();
    imageViewForGif.setVisibility(View.VISIBLE);
    frameAnimation.start();

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            imageViewForGif.setVisibility(View.INVISIBLE);
        }
    }, 2000);
}

but compiler doesn't recognize new Handler() and postDelayed. I don't know what I have done wrong

Upvotes: 0

Views: 354

Answers (3)

bricklore
bricklore

Reputation: 4165

Try to use this import statement:

import android.os.Handler;

instead of import android.util.logging.Handler;

What you are searching for is androids Handler which manages thread queues. Java's logging handler is actually nothing you should worry about. From the documentation:

android: There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.


java: A Handler object accepts a logging request and exports the desired messages to a target, for example, a file, the console, etc

Upvotes: 0

Anton
Anton

Reputation: 461

Try using this class:

import android.os.Handler;

Upvotes: 1

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

You have to import Handler from

import android.os.Handler;

instead of

import java.util.logging.Handler;

I hope it helps!

Upvotes: 2

Related Questions