Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a progress bar on a image view programatically

I have defined a image view programatically and updating it asynchronously with the images downloaded from the webservice.

Now, i want to add a progress bar to image view so that the image view will show the progress bar till the image is loaded. I have not defined the imageview in the xml file because i am updating it inside a horizontal scroll view.

Please help need it desperately...!!!

Here's the code that i am updating the image view

protected ImageView asImageView(final String message,final String path){
    final ImageView iv=new ImageView(this);


    AsyncImageLoaderv asyncImageLoaderTop = new AsyncImageLoaderv();
    Bitmap cachedImageTop=
    asyncImageLoaderTop.loadDrawable(
            path, new AsyncImageLoaderv.ImageCallback() {
                public void imageLoaded(Bitmap imageDrawable,
                        String imageUrl) {

                    iv.setImageBitmap(imageDrawable);
                }
            });
    iv.setImageBitmap(cachedImageTop);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    LinearLayout.LayoutParams params=  new LinearLayout.LayoutParams(100,120);
    params.setMargins(10, 10, 10, 10);
    iv.setLayoutParams(params);

    iv.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            startActivity(new Intent(getApplicationContext(),
                    ShowPhotoActivity.class).putExtra("imagePath",
                    path).putExtra("message", message));


        }
    });
    return iv;
}
like image 238
Rahul Kalidindi Avatar asked Apr 28 '26 08:04

Rahul Kalidindi


2 Answers

I know this is a few years old, but I figured I'd still post as it might help someone in the future...

You should use the library provided by Koush. It is extremely simply and straightforward to use. Plus it handles all the complicated stuff for you automatically!

https://github.com/koush/UrlImageViewHelper

Documentation and further explanation is available at the URL above - no need to reproduce here.

like image 107
Stephen Avatar answered Apr 29 '26 21:04

Stephen


I've just had a similar problem. I'm not sure you can insert a ProgressBar on an ImageView, but you can display it in place of the imageview, and then hide it using setVisibility(View.GONE). I see your function returns ImageView. Perhaps it could return View type, and then if no image is present, it could return a ProgressBar which is also a View? Alternatively, you could create and use an AnimationDrawable of yours.

like image 43
Yar Avatar answered Apr 29 '26 21:04

Yar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!