score:207

Accepted answer

the previously accepted method has been deprecated, according to the sdk 22 documentation:

prior to android.os.build.version_codes#jelly_bean, this function would not correctly retrieve the final configuration density when the resource id passed here is an alias to another drawable resource. this means that if the density configuration of the alias resource is different than the actual resource, the density of the returned drawable would be incorrect, resulting in bad scaling.

as pointed out in this answer better solution would be to use contextcompat: contextcompat.getdrawable(context, r.drawable.***)

score:1

you should use " getdrawable(id, this.gettheme()) ". this method is not deprecated till now.

if (build.version.sdk_int >= build.version_codes.lollipop) {
    view.setbackground(getresources().getdrawable(r.drawable.radioline,this.gettheme()));
} else {
   view.setbackground(getresources().getdrawable(r.drawable.radioline));
}

score:1

i agree using contextcompact.getdrawable(context context, int resid). it worked for me and my app targets api 19.

score:1

you can also set the resource directly without working with the drawable (kotlin):

btn.setimageresource(r.drawable.ic_xxx)

score:2

appcompatresources.getdrawable(context, r.drawable.*)

score:2

solution for kotlin programmers looks like:

val greenprogressbar = context!!.getdrawable(r.drawable.custom_progressbargreen)

or (from api 22)

val greenprogressbar = contextcompat.getdrawable(r.drawable.custom_progressbargreen)

score:9

drawable greenprogressbar = context.getresources().getdrawable(r.drawable.custom_progressbargreen);

score:12

i had a same situation which i wanted to reference getdrawable() method which is now deprecated.

what i used:

mybutton.setimagedrawable(contextcompat.getdrawable(getactivity(), r.drawable.ic_btn_off));

score:26

add a getresources() after the context:

drawable greenprogressbar = context.getresources().getdrawable(r.drawable.custom_progressbargreen);

Related Query

More Query from same tag