Nick
Nick

Reputation: 9373

Android: Progress Bar as a place holder

I was hoping to figure out how to use a progress bar spinner to be a place holder while my image is being fetched. I put an animated .gif in but soon after I found out that .gif don't play nice with android. Here is my code now:

public void displayImage(String url, CarActivity activity, ImageView imageView) {
        if(imageMap.containsKey(url))
            imageView.setImageBitmap(imageMap.get(url));
        else {
            queueImage(url, activity, imageView);
            imageView.setImageResource(R.drawable.loading);

        }
    }

Instead of setting the image to my loading.gif how can I use the built in spinner as a place holder and possibly have it overlaid on another image.

thanks!

Upvotes: 1

Views: 3159

Answers (2)

theisenp
theisenp

Reputation: 8719

I would look into using a ViewAnimator to switch between your ImageView and a ProgressBar widget.

Add the ViewAnimator to your XML Layout, and then do something like this in your Activity:

ViewAnimator switch = (ViewAnimator) findViewById(R.id.your_id);
ImageView image = new ImageView(this);
ProgressBar loading = new ProgressBar(this);

switch.addView(loading, 0, myLayoutParams);
switch.addView(image, 1, myLayoutParams);

Where the first field of addView() is the Child View you are adding, the second is it's index in the Animator, and the third is a LayoutParams object that you have defined (which I didn't show).

I haven't tested the code, but that should hopefully display the Android ProgressBar widget.

Then, whenever your image has loaded, call:

switch.showNext();

Also, the example here seems to be similar to what you need if you want to check it out.

Upvotes: 2

mah
mah

Reputation: 39807

Your real question is "how can I have one View placed above another View". In your case, it seems you want an ImageView in the back and a ProgressBar in the front. (The ProgressBar name is a bit unintuitive because by default it's not a bar, it's a small spinning animation).

In your layout, where you currently have your ImageView, create a RelativeLayout. Within the RelativeLayout, first place your ImageView, and then place your ProgressBar. Set the ProgressBar's android:visibility attribute = "invisible", and when you want it to be seen, set it visible within your code: view.setVisibility(View.VISIBLE); (or .INVISIBLE).

Set the RelativeLayout's size to wrap_content, and do the same for the ImageView. For the ProgressBar, you may wish to hard code a small size, say 40dip width and also height. Set the layout parameters of the ProgressBar to "centerInParent" if you want it centered over the middle of the image.

Upvotes: 5

Related Questions