For example I get three images in response from a web service and I want to show one image at a time in screen ..After every ten seconds image should change itself....how this can be done?
Asked
Active
Viewed 119 times
-2
-
I did not exactly know how to do it?? can you put a little example here??@BharathMg – Muhammad Zeshan Sep 02 '14 at 11:46
-
refer http://stackoverflow.com/questions/22860546/android-changing-image-with-time-interval – Ritesh Gune Sep 02 '14 at 11:48
1 Answers
1
The code under the run(){} method will repeat after a interval of time mentioned.
// make global variables
int 1=0;
private Handler handler;
Timer timer;
Runnable mUpdateResults;
// Declare ArrayList of type Bitmaps
final ArrayList<Bitmap> images = new ArrayList<Bitmap>();
// get the images from drawable folder and convert it into bitmaps plus add them in arrayList
images.add(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher));
images.add(BitmapFactory.decodeResource(this.getResources(), R.drawable.one));
images.add(BitmapFactory.decodeResource(this.getResources(), R.drawable.two));
// get the imageview
final ImageView a = (ImageView)findViewById(R.id.imageView1);
timer = new Timer();
handler = new Handler();
mUpdateResults = new Runnable() {
public void run() {
// checking if i==0 so set first image.. similarly so on
if(i == 0)
{
a.setImageBitmap(images.get(0));
}
if(i ==1)
{
a.setImageBitmap(images.get(1));
}// .. add more if you like
i++;
}
}
};
int delay = 1000 ; // you can adjust it according to your need
int period = 8000; // you can adjust it according to your need
// execute timer
timer.scheduleAtFixedRate(new TimerTask() {
public void run(){handler.post(mUpdateResults); }}, delay, period);
for stopping the timer
handler.removeCallbacksAndMessages(mUpdateResults);
timer.cancel();
here
delay--This is the delay in milliseconds before task is to be executed.
period--This is the time in milliseconds between successive task executions.

Jamil
- 5,457
- 4
- 26
- 29
-
you mean url is string here and you want to store string(images urls) in an array? – Jamil Sep 02 '14 at 11:51
-
You can use ArrayList
images = new ArrayList – Jamil Sep 02 '14 at 11:54(); and images.add(bitmap); to store images in arraylist -
for string you can store like ArrayList
imagesName = new ArrayList – Jamil Sep 02 '14 at 11:55(); .. imagesName.add("image1"); -
and you can use like this=== if(i == 1){imageView.setBitmapImage(yourimage1)} else if(i == 2){ and so on – Jamil Sep 02 '14 at 11:57
-
Plz Droid can you upload a full sample code so that I approve your answer – Muhammad Zeshan Sep 02 '14 at 12:00
-
check my edited answer.. i have tested it and it is fully functional.. please consider accepting answer or upvoting it.. – Jamil Sep 02 '14 at 12:23
-
Yes That working good thanks... I am accepting it just one more thing please if i want associate each image a url like in response i get two things an image and a url ...after clicking image .... url associated with that image called and open in browser – Muhammad Zeshan Sep 02 '14 at 12:45
-
you mean on imageclick , get that image url and by url show it in browser(webview) ? – Jamil Sep 02 '14 at 12:49
-
Yes on image click ...image url click which is given in response from each image has it's unique url – Muhammad Zeshan Sep 02 '14 at 12:51
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60454/discussion-between-droidcoder-and-muhammad-zeshan). – Jamil Sep 02 '14 at 13:18