0

I am working on android app and i have a images slider in my app,now i want to when i click on my slider image,the images are display in another activity but it takes a little time while to open,now i want to implement and add a loading progress bar when display the image in another activity,can anyone tell me how i can implement progress bar when i click my image slider and when open another activity it's start progressbar and when images are display it's dimiss?any help,idea and sugession will be much appreciated,Thanks in advance.

This is my activity when i click on my image and anothe activity open:

iv_openimage = (ImageView) findViewById(R.id.iv_openimage);
iv_openimage.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(VarientDetails.this,ImageSwitcher.class);

        intent.putExtra("imageurls", imageurls);
        Log.d("CMH", "images url = " + imageurls);
        startActivity(intent);
    }
});

and this is my imageswitcher activity were i can display my images:

public class ImageSwitcher extends Activity {

ImageView iv_getimage;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitcher);

iv_getimage = (ImageView) findViewById(R.id.imageView1);
ArrayList<String> resultArray = getIntent().getStringArrayListExtra("imageurls");
Log.d("CMH imgswtchr", "images url = " + resultArray);
Picasso.with(getBaseContext()).load(resultArray.get(0))
            .into(iv_getimage);

}

}
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
  • Start ProgressDialog onTouchListener of your imageview and dismiss it when you call setBackground() method to set new image based on swipe. – AndiM Jan 17 '14 at 07:14
  • @AndiM but i want it to display my progress bar when images are not display,when image is display then progress bar is dismiss,i have edited my ans and checked my imageswitcher activity and let me know were i can implemnt my progress bar. – Farhan Shah Jan 17 '14 at 07:39
  • @AndiM i have edit my ans plz checked and let me know,now how i can implemnt progress bar in imageswitcher activity? – Farhan Shah Jan 17 '14 at 07:42

1 Answers1

0

What you want to do is put the resource intensive tasks in a so called AsyncTask. This AsyncTask runs on a different thread then the UI thread. Put it can do UI operations in it's onPostExecute(); So what you want is to do something along the lines of:

start activity
set progressbar visible
start asynctask

In the AsyncTask:

execute image task that takes up time
hide progressbar

For more info about AsyncTask you can go here: http://developer.android.com/reference/android/os/AsyncTask.html

Also, the reason that your current implementation isn't optimal is because it all runs on the same thread. This makes the UI thread unavailable at a certain time. This is bad practice, and gives the user a bad experience. So I'd always recommend using an AsyncTask for operations like this. While it is possible to show the progressbar before and after the task is done, the task would still lock the UI thread.

Edit

As requested, here is something I'd do in your situation:

I'd first create an inner class that extends AsyncTask like this

private class ImageLoadTask extends AsyncTask<String, Param, String>
{

    @Override
    protected String doInBackground(String... params)
    {
         //do something to retrieve the images
    }

    @Override
    protected void onPostExecute(String result)
    {
        //hide the progressbar
    }
}

Then in the Activity itself to start this AsyncTask simply do:

progressBar.setVisibility(View.Visible);
ImageLoadTask imgTask = new ImageLoadTask();
imgTask.execute("parameter");
Tim Kranen
  • 4,202
  • 4
  • 26
  • 49