display NetworkImageView's default image without network request
Asked Answered
I

4

14

I have a ListView displaying things, many of which have an illustrative image and some of which don't. The ViewHolder uses NetworkImageView to display the illustrations.

The problem is that even if a default image is set, it won't be displayed until one calls setImageUrl(), which in turn will set the source bitmap of the view to null if the url is empty. If the url isn't empty, it makes a request. This means that one is forced to make a network request even if there isn't a valid network url associated with that particular view, otherwise instead of displaying the default image it displays an empty view.

(issue filed: https://code.google.com/p/android/issues/detail?id=59538)

Is there a way around making bogus network requests for items without a valid url?

Icky answered 5/2, 2014 at 1:35 Comment(1)
The best I can come up with so far is swapping out the NetworkImageView with a vanilla ImageView at runtime, which is arguably slightly less inefficient than extra network requests but still inefficient.Icky
H
39

This works just fine for me:

    String url = getURL();
    NetworkImageView niv = (NetworkImageView)rootView.findViewById(R.id.niv);
    if(url.length() > 0)
        niv.setImageUrl(url, imageLoader);
    niv.setDefaultImageResId(R.drawable._default);
    niv.setErrorImageResId(R.drawable.error);
Hellion answered 8/2, 2014 at 10:1 Comment(5)
It actually displays the default image even if the url isn't set?Icky
@Icky yep. Make sure are you're using the lasted Volley's code.Hellion
You're right, they already fixed this. I just hadn't pulled the latest version.Icky
What's the point of extending ImageView if they're going to break setImageDrawable?Christcrossrow
I use the same code but the default image not shown sometime. Instead it shows the image in xml file. What can I do?Rahn
J
1

For NetworkImageView there is a variable defined named as mDefaultImageId You can use this for defining default image's resource

Here is how you can do it-

Create a file named attrs.xml, in that put this lines -

<declare-styleable name="DefaultImageResId">
    <attr name="default_image_resource" format="reference" />
</declare-styleable>

Create a class named CustomNetworkImageView and use following code -

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.android.volley.toolbox.NetworkImageView;
import com.myapp.R;

public class CustomNetworkImageView extends NetworkImageView {
    public CustomNetworkImageView(Context context) {
        super(context);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDefaultImageResId(context, attrs);
    }

    public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDefaultImageResId(context, attrs);
    }

    private void setDefaultImageResId(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DefaultImageResId);
        int resId = typedArray.getResourceId(R.styleable.DefaultImageResId_default_image_resource, 0);

        if (0 != resId)
            setDefaultImageResId(resId);
    }
}

Now in your regular layout xml file use like this -

<com.myapp.CustomNetworkImageView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/img"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:default_image_resource="@drawable/defaultimage"/>

Remember, line xmlns:app="http://schemas.android.com/apk/res-auto" is very important at the root element of your layout file Then add app:default_image_resource

You are done!!! Now you can even mention image from your xml

Juliettajuliette answered 17/10, 2015 at 8:13 Comment(0)
F
0

may be you can try android:background="yourdrawable"

<com.android.volley.toolbox.NetworkImageView
                    android:id="@+id/puppy_imageView"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:scaleType="centerCrop"
                    android:background="@drawable/drawablename"

                    />

UPDATE ANSWER

after I try this answer I figure the new image will override background so its not good answer

According to documentation setDefaultImageResId can do this

Sets the default image resource ID to be used for this view until the attempt to load it completes.

simply when you define your

NetworkImageView _ImageView = (NetworkImageView) findViewById(R.id.imageview);
_ImageView.setDefaultImageResId(R.drawable.yourdrawable);
Fazeli answered 29/11, 2015 at 19:36 Comment(0)
C
0

ImageView is a superclass so use its own setter method to get started:

niv.setImageResource(R.drawable.icon_no_image_avail);   // coerce NetworkImageView to draw itself initially.
Capitulary answered 15/6, 2016 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.