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