I'm trying to put an image to my ImageView
, with a custom attribute defined by me, I'm doing it this way:
Attrs:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="MyAttr">
<attr name="my_image" format="reference"/>
</declare-styleable>
</resources>
ImageView Attrs:
app:my_image="@drawable/image"
Than in my View
:
int imageSrc;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyAttr, 0, 0);
try {
imageSrc = ta.getResourceId(R.styleable.MyAttr_my_image, -1);
} finally {
ta.recycle();
}
And set the image to the ImageView
with:
imageView.setImageResource(imageSrc);
But nothing appears, I've tried also with:
imageSrcDrawable = ta.getDrawable(R.styleable.MyAttr_my_image);
And:
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
imageView.setBackgroundDrawable(imageSrcDrawable);
} else {
imageView.setBackground(imageSrcDrawable);
}
UPDATE:
I've already tried to parse attribute with ta.getInt(..)
and its work fine!
I can not understand why, thanks in advance!