java - setImageResource() with attrs value -


i'm trying put image imageview, custom attribute defined me, i'm doing 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 view:

int imagesrc; typedarray ta = context.obtainstyledattributes(attrs, r.styleable.myattr, 0, 0); try {     imagesrc = ta.getresourceid(r.styleable.myattr_my_image, -1); } {     ta.recycle(); } 

and set image imageview with:

imageview.setimageresource(imagesrc); 

but nothing appears, i've tried 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 tried parse attribute ta.getint(..) , work fine!

i can not understand why, in advance!

it have been useful if had included layout xml code, however, i'm going take stab in dark , suggest might have problem name-spacing. have defined namespace in layout xml as

xmlns:whatever="http://schemas.android.com/apk/res/com.yourcompany.yourpackage" 

if not, i'd give spin. other suggestions:

i believe obtainstyledattributes resolves attributes when call on attributeset. so, wouldn't surprised if typedarray instance contains resolved resource id, in short: try , use

imagesrc = ta.getint(r.styleable.myattr_my_image, -1); 

as opposed ta.getresourceid(). , finally, if above fails, try , use ta.hasvalue(r.styleable.myattr_my_image) determine if value exist, if not, atleast know obtainstyledattributes didn't parse , resolve attribute, , thus, can start looking why. in, file locations, namespaces etc.

hope working.

edit:

after reading comments, got 1 last question, snippets above, can't see method initialising in, , i'm wondering if image view being redrawn afterwards. i'm presuming imageview.setimageresource(imagesrc); invalidates imageview, however, since showing up, , confirmed 100% sure loaded correctly, might worth invalidating manually, check, trying imageview.invalidate(); after calling image.setimageresource().


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -