-2

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?

nobalG
  • 4,544
  • 3
  • 34
  • 72
Muhammad Zeshan
  • 104
  • 3
  • 13

1 Answers1

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(); and images.add(bitmap); to store images in arraylist – Jamil Sep 02 '14 at 11:54
  • for string you can store like ArrayList imagesName = new ArrayList(); .. imagesName.add("image1"); – Jamil Sep 02 '14 at 11:55
  • 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