Dragon
Dragon

Reputation: 453

Android: Show loading symbol in Network ImageView

I am using NetworkImageView from Volley library to load images.

But while the image takes time load from a URL I want to show a spinning/moving loading symbol in the image container.

I tried using the setDefaultImageResId() function to set a loader.gif image. But I think gif format is not supported in Android natively.

Please advice. Thanks.

Upvotes: 4

Views: 7617

Answers (3)

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

I have done this using animation drawable. Here are the steps:

Animate drawable ic_sync_drawable.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/selected"
    android:oneshot="false">
    <item
        android:drawable="@drawable/ic_popup_sync_1"
        android:duration="50" />
    <item
        android:drawable="@drawable/ic_popup_sync_2"
        android:duration="50" />
    <item
        android:drawable="@drawable/ic_popup_sync_3"
        android:duration="50" />
    <item
        android:drawable="@drawable/ic_popup_sync_4"
        android:duration="50" />
    <item
        android:drawable="@drawable/ic_popup_sync_5"
        android:duration="50" />
    <item
        android:drawable="@drawable/ic_popup_sync_6"
        android:duration="50" />
</animation-list>

Now in java code I am using ImageView:

Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_sync_drawable);
ImageView imageView = new ImageView(getContext());
imageView.setImageDrawable(drawable);
imageLoader.get(imageurl, ImageLoader.getImageListener(imageView, 0, 0));
ViewFlipper.addView(imageView);
//start animation
 AnimationDrawable frameAnimation = (AnimationDrawable) drawable;
frameAnimation.start();

Upvotes: 1

manuelvsc
manuelvsc

Reputation: 525

You Just need to put the NetworkImageView and the ProgressBar inside a FrameLayout or a RelativeLayout,something like this

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

     <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="..."
            android:layout_height="..." />

    <com.android.volley.toolbox.NetworkImageView
        android:layout_width="..."
        android:layout_height="..."
         />

</FrameLayout>

be aware that the ORDER COUNTS, make sure you put the NetworkImageView after the ProgressBar so that the NetworkImageView stays on top of the ProgressBar after loading the image.

Hope it helps!

Upvotes: 9

Itai Hanski
Itai Hanski

Reputation: 8690

You are correct. Animated gifs are not really supported in Android. But the good news is that you can use the built in progress bar/wheel. Read about it here.

You'll have to switch the NetworkImageView you've been using with a regular ImageView and load your image using the ImageLoader. That way you can implement a listener to switch out the progress wheel. This means you'll need to create a RequestQueue and an ImageLoader. I advise you to create one of each and share them via a singleton class with whoever needs it in your code.

The image loading should look something along the lines of:

// ** code in init the progress wheel **

imageLoader.get(url, new ImageListener() {
    @Override
    public void onResponse(ImageContainer response, boolean isImmediate) {
        if (response != null) {
            Bitmap bitmap = response.getBitmap();
            if (bitmap != null) {
                // ** code to turn off the progress wheel **
                // ** code to use the bitmap in your imageview **
            }

    @Override
    public void onErrorResponse(VolleyError error) {
        // ** code to handle errors **
    }
});

Upvotes: 3

Related Questions