How to change the image dynamically in ImageView

Tutorials with advanced 'difficulty' and more Lines of Code.

How to change the image dynamically in ImageView

Postby aptgopal » Tue Dec 07, 2010 5:20 pm

Hi
I am developing my android application in that apps i want to change the image available in the image view will be change dynamically .I am downloading the image from my web server .I want to change the image available in the ImageView will be change in the particular period of time

Thanks in Advance
aptgopal
Developer
Developer
 
Posts: 25
Joined: Tue Nov 16, 2010 7:08 pm
Location: India

Top

Re: How to change the image dynamically in ImageView

Postby alideo » Thu Dec 09, 2010 12:58 pm

You want to change the image of the imageview after the image is downloaded?
AliDeo
alideo
Freshman
Freshman
 
Posts: 5
Joined: Thu Mar 04, 2010 9:04 pm

Re: How to change the image dynamically in ImageView

Postby aptgopal » Mon Dec 20, 2010 10:03 pm

HI AliDeo
Yes I want to change the image after downloading
aptgopal
Developer
Developer
 
Posts: 25
Joined: Tue Nov 16, 2010 7:08 pm
Location: India

Re: How to change the image dynamically in ImageView

Postby Exoit » Tue Dec 21, 2010 1:26 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.Slideshow;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. import android.app.Activity;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10.  
  11. public class slideshow extends Activity {
  12.         /** Called when the activity is first created. */
  13.  
  14.         private long delayInMili = 3000;
  15.         private Timer mTimer;
  16.  
  17.         @Override
  18.         public void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.main);
  21.         }
  22.  
  23.         @Override
  24.         protected void onPause() {
  25.                 super.onPause();
  26.                 if (mTimer != null) {
  27.                         mTimer.cancel();
  28.                         mTimer.purge();
  29.                 }
  30.         }
  31.  
  32.         @Override
  33.         protected void onResume() {
  34.                 super.onResume();
  35.                 mTimer = new Timer();
  36.                 mTimer.schedule(new TimerTask() {
  37.                         @Override
  38.                         public void run() {
  39.                                 new Downloader().execute("URL");
  40.                         }
  41.                 }, 0, delayInMili);
  42.         }
  43.  
  44.         private class Downloader extends AsyncTask<String, Void, Drawable> {
  45.                 @Override
  46.                 protected Drawable doInBackground(String... URL) {
  47.                         // Download picture
  48.                         return null;
  49.                 }
  50.  
  51.                 @Override
  52.                 protected void onPostExecute(Drawable image) {
  53.                         super.onPostExecute(image);
  54.                         // Update the ImageView
  55.                 }
  56.         }
  57. }
  58.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


To set your downloaded image to the ImageView you can create a Drawable by using Drawable.createFromStream and then set the Drawable to the ImageView with setImageDrawable. It was not clear from your post if you needed help with creating the rest of the app, so i wrote a short code snippet of how i would approach the problem.
Exoit
Freshman
Freshman
 
Posts: 4
Joined: Sun Dec 19, 2010 1:00 am

Re: How to change the image dynamically in ImageView

Postby aptgopal » Tue Dec 21, 2010 6:39 pm

Hi Exoit

Thanks for your reply I got the idea .One more thing If internet connection is not available means i want to hide the image view is it possible and where to set the URL'S of the downloading image in code .Is their any possibility to set the link for each image

Don't mistake me because I am new to android development
aptgopal
Developer
Developer
 
Posts: 25
Joined: Tue Nov 16, 2010 7:08 pm
Location: India

Re: How to change the image dynamically in ImageView

Postby Exoit » Wed Dec 22, 2010 12:44 am

To hide a view checkout setVisibility in the SDK. In the code example i gave you would set the url in the execution of the downloader e.g. Downloader().execute("http://example.org/example.png");
Exoit
Freshman
Freshman
 
Posts: 4
Joined: Sun Dec 19, 2010 1:00 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests