Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay actions in android

Tags:

android

I want to change the image in imageView after 5 seconds from app start.

This is the code that I tried so far:

public class MainActivity extends Activity {
ImageView screen;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    screen=(ImageView)findViewById(R.id.imageView1);

    screen.setImageResource(R.drawable.ic_launcher);

    }
}
like image 591
A.Jouni Avatar asked Jan 06 '13 21:01

A.Jouni


People also ask

How to provide delay in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2− Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, primary it shows "initial text" after some delay it will update with new text.

What is long press duration?

But, the long press duration is customizable globally by setting it in accessibility. Values are Short (400 ms), Medium (1000 ms) or Long (1500 ms).

What is Handler in Android example?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .


2 Answers

You can use a Handler, such as:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        // change image
    }

}, 5000); // 5000ms delay

As Sam says in the comments, you could also do this (because all Views have their own handler):

screen.postDelayed(new Runnable() {

    @Override
    public void run() {
        // change image
    }

}, 5000); // 5000ms delay

See the Handler Documentation.

like image 180
Tom Leese Avatar answered Oct 17 '22 21:10

Tom Leese


you can try thread like this:

 new Thread(){  
        public void run(){  
            //sleep(5000);
            //refreshSthHere();
        }  
    }.start();  
like image 26
Winnie Avatar answered Oct 17 '22 22:10

Winnie