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);
}
}
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.
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).
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 .
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 View
s have their own handler):
screen.postDelayed(new Runnable() {
@Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
See the Handler Documentation.
you can try thread like this:
new Thread(){
public void run(){
//sleep(5000);
//refreshSthHere();
}
}.start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With