android getdrawable theme,Android getResources().getDrawable() deprecated API 22

1173

You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:

A) drawables with theme attributes

ContextCompat.getDrawable(getActivity(), R.drawable.name);

You'll obtain a styled Drawable as your Activity theme instructs.

This is probably what you need.

B) drawables without theme attributes

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable() is not deprecated!

EXTRA) drawables with theme attributes from another theme

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

Follow

bb8ea97cddc30bf06bdcc04f9f76e5c1.png

Alex Lockwood

81.3k3737 gold badges197197 silver badges245245 bronze badges

answered Mar 19 '15 at 14:13

7994dfd82e33bf4095cee6cbd0b87750.png

araksaraks

38.6k88 gold badges3232 silver badges3939 bronze badges

5

My app crashes using suggestion B. It doesn't like the call Drawable originalIcon = ResourcesCompat.getDrawable(ctxt.getResources(), iconResId, null);

– FractalBob

May 29 '17 at 21:54

I declare it this way: public static void setImageButtonEnabled(Context ctxt, boolean enabled, ImageButton item, int iconResId) { item.setEnabled(enabled); Drawable originalIcon = ResourcesCompat.getDrawable(ctxt.getResources(), iconResId, null); Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon); item.setImageDrawable(icon); } and call it like this: Utility.setImageButtonEnabled(getContext(), false, back, R.drawable.arrow_left);

– FractalBob

May 29 '17 at 21:58

More precisely, the crash seems to be happening because my icon is a Vector drawable.

– FractalBob

May 29 '17 at 22:58

1

xamarin version: ResourcesCompat.GetDrawable(Resources, Resource.Drawable.name, null);

– Brian

Oct 23 '17 at 12:20

I add one further word to you,ContextCompat.getColor(context,color) also can help you...

– BertKing

Aug 21 '18 at 12:32

750

Edit: see my blog post on the subject for a more complete explanation

You should use the following code from the support library instead:

ContextCompat.getDrawable(context, R.drawable.***)

Using this method is equivalent to calling:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

return resources.getDrawable(id, context.getTheme());

} else {

return resources.getDrawable(id);

}

As of API 21, you should use the getDrawable(int, Theme) method instead of getDrawable(int), as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null).

Follow

answered Mar 13 '15 at 20:32

bb8ea97cddc30bf06bdcc04f9f76e5c1.png

Alex LockwoodAlex Lockwood

81.3k3737 gold badges197197 silver badges245245 bronze badges

6

8

I think that the OP also refers to the getDrawable (int id) method of the Context class. This is the same as getResources().getDrawable(id, getTheme()); and also uses the new API.

– code monkey

Mar 13 '15 at 21:04

9

According to documentation, min API level 21 is required to use getDrawable(int, Resources.Theme).

– Prince

May 4 '15 at 5:47

@Prince The method was added in API 21 but it wasn't deprecated until API 22. :)

– Alex Lockwood

Aug 26 '15 at 13:59

Android doc also recommends using Context::getDrawable(int) method but since it was introduced only in API 21 seems that ContextCompat is the best choice.

– goRGon

Jan 12 '16 at 1:22

This answer does not work with .svg files, in versions older than API 21. There is a bug in the library.

– Jorge Rodríguez

Mar 17 '16 at 20:47

145

Replace this line :

getResources().getDrawable(R.drawable.your_drawable)

with ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

EDIT

ResourcesCompat is also deprecated now. But you can use this:

ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context)

for more details follow this link: ContextCompat

Follow

answered Mar 18 '15 at 8:32

4f252b4fab9826e5797e585ffa5e67f7.png

vincent091vincent091

2,24544 gold badges1515 silver badges2121 bronze badges

4

2

It doesn't say in link that ResourcesCompat is deprecated. It should work fine.

– Jacob R

Jun 9 '15 at 10:29

what to write in "your drawable" if i want to input images to a list view on clicking list items stored in drawable

– 0x0001

Dec 5 '16 at 8:35

29

getResources().getDrawable() was deprecated in API level 22. Now we must add the theme:

This is an example:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

This is an example how to validate for later versions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

} else {

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage));

}

Follow

answered Sep 29 '15 at 21:25

a2e3a640c5d4cfc56f4e4f538ae3d41a.png

JorgesysJorgesys

114k2222 gold badges306306 silver badges247247 bronze badges

1

Build.VERSION_CODES.LOLLIPOP is API 21, so shouldn't this be if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) or if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)? Never mind. From below "The method was added in API 21 but it wasn't deprecated until API 22. :)"

– Mark Cramer

May 17 '16 at 18:16

5

In Kotlin you can use extension

fun Context.getMyDrawable(id : Int) : Drawable?{

return ContextCompat.getDrawable(this, id)

}

then use like

context.getMyDrawable(R.drawable.my_icon)

Follow

answered Nov 12 '19 at 4:42

eca5f592fef4a19e799323065f923385.png

Deepak RorDeepak Ror

1,40822 gold badges1515 silver badges2121 bronze badges

3

You can use

ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);

that's work for me

Follow

answered Dec 26 '16 at 4:36

picture?type=large

Dasser BasyouniDasser Basyouni

2,61433 gold badges2020 silver badges4343 bronze badges

3

Using application context to load vector drawable crashes on pre lollipop. Any fix for this?

– muthuraj

Feb 9 '18 at 7:05

1

@muthuraj I don't remember my testing for the code with lollipop case, but you can try getActivity() or getResources() instead then, does that goes well with your code ?

– Dasser Basyouni

Feb 24 '18 at 11:24

I'm using MVVM pattern and I need to inflate drawables inside ViewModels which don't have activity context. It only has application context. That's what my issue is.

– muthuraj

Feb 24 '18 at 20:46

3

getDrawable(int drawable) is deprecated in API level 22.

For reference see this

Now to resolve this problem we have to pass a new constructer along with id like as :-

getDrawable(int id, Resources.Theme theme)

For Solutions Do like this:-

In Java:-

ContextCompat.getDrawable(getActivity(), R.drawable.name);

or

imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));

In Kotlin :-

rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)

or

rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)

Hope this will help you.Thanks.

Follow

answered Oct 11 '19 at 5:21

b6198769ae473e4888d70d5c507885cb.png

Rahul KushwahaRahul Kushwaha

1,13611 gold badge99 silver badges1919 bronze badges

1

It is worth mentioning, that getDrawable(DrawableRes int id, Theme theme) can throw an exception if the resource is not found while, getDrawable(Context context, int id) is nullable, so it has to be returned with Drawable? in Kotlin.

– Pitos

May 26 '20 at 5:55

1

Just an example of how I fixed the problem in an array to load a listView, hope it helps.

mItems = new ArrayList();

// Resources resources = getResources();

// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.az_lgo), getString(R.string.st_az), getString(R.string.all_nums)));

// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.ca_lgo), getString(R.string.st_ca), getString(R.string.all_nums)));

// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.co_lgo), getString(R.string.st_co), getString(R.string.all_nums)));

mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.az_lgo, null), getString(R.string.st_az), getString(R.string.all_nums)));

mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.ca_lgo, null), getString(R.string.st_ca), getString(R.string.all_nums)));

mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.co_lgo, null), getString(R.string.st_co), getString(R.string.all_nums)));

Follow

answered Jan 12 '16 at 17:40

97703b4956912d2b2afc379e80e7908f?s=32&d=identicon&r=PG&f=1

JayJay

1911 bronze badge

1

Try this:

public static List getCatalog(Resources res){

if(catalog == null) {

catalog.add(new Product("Dead or Alive", res

.getDrawable(R.drawable.product_salmon),

"Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));

catalog.add(new Product("Switch", res

.getDrawable(R.drawable.switchbook),

"Switch by Chip Heath and Dan Heath", 24.99));

catalog.add(new Product("Watchmen", res

.getDrawable(R.drawable.watchmen),

"Watchmen by Alan Moore and Dave Gibbons", 14.99));

}

}

Follow

2e017633cda5465e6683c74992cdec5e?s=32&d=identicon&r=PG

Donald Duck

6,4881818 gold badges5959 silver badges7979 bronze badges

answered Feb 9 '17 at 14:47

0661888dd447dcef3b58cc9ccc2485e1?s=32&d=identicon&r=PG&f=1

syakirin mohd norsyakirin mohd nor

1111 bronze badge

1

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

– Donald Duck

Feb 9 '17 at 15:06

1

If you are targeting SDK > 21 (lollipop or 5.0) use

context.getDrawable(R.drawable.your_drawable_name)

Follow

answered Sep 24 '18 at 20:21

aajgs.jpg?s=32&g=1

Adeel AhmadAdeel Ahmad

82911 gold badge99 silver badges1818 bronze badges

0

en api level 14

marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));

Follow

7db5b712efab2db3ab15e3f585f6e095?s=32&d=identicon&r=PG

ColdFire

6,11666 gold badges3232 silver badges5050 bronze badges

answered Jan 20 '18 at 16:46

picture?type=large

Jaime López RomeroJaime López Romero

1

1

You should provide a bit more context around your answer.

– Nicolas Grenié

May 3 '18 at 21:01

0

Now you need to implement like this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21

//

} else {

//

}

Following single line of code is enough, everything will take care by ContextCompat.getDrawable

ContextCompat.getDrawable(this, R.drawable.your_drawable_file)

Follow

answered Mar 1 '19 at 4:20

24b60dd0d6855072619355ca3b9e09e1?s=32&d=identicon&r=PG&f=1

Farid HaqFarid Haq

1,9191515 silver badges1313 bronze badges

0

For some who still got this issue to solve even after applying the suggestion of this thread(i used to be one like that) add this line on your Application class, onCreate() method

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)

As suggested here and here sometimes this is required to access vectors from resources especially when you're dealing with menu items, etc

Follow

answered Oct 30 '19 at 8:24

picture?type=large

Stamatis StiliatsStamatis Stiliats

36966 silver badges1919 bronze badges

0

In case you need drawable from other app targeting SDK 23 and up

PackageManager manager = getApplicationContext().getPackageManager();

Resources resources = null;

try {

resources = manager.getResourcesForApplication("com.anyapp");

}

catch (PackageManager.NameNotFoundException e) {

e.printStackTrace();

}

assert resources != null;

Drawable notiIcon = ResourcesCompat.getDrawable(resources, current.iconId/* drawable resource id */, null);

Follow

answered Feb 28 at 5:41

edeee27c53503c8672d29903dde62470.png

Khalid LakhaniKhalid Lakhani

14411 silver badge77 bronze badges

0

Try this

ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);

Follow

answered May 3 at 9:52

64e8e6e5de4df100a03f119afba3d845.png

Aalishan AnsariAalishan Ansari

31311 silver badge44 bronze badges

-2

Build.VERSION_CODES.LOLLIPOP should now be changed to BuildVersionCodes.Lollipop

i.e:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {

this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder, Context.Theme);

} else {

this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder);

}

Follow

7db5b712efab2db3ab15e3f585f6e095?s=32&d=identicon&r=PG

ColdFire

6,11666 gold badges3232 silver badges5050 bronze badges

answered Dec 2 '15 at 1:06

picture?type=large

Ryan HermanRyan Herman

1333 bronze badges

1

Isn't BuildVersionCodes a class specific to Xamarin?

– Ted Hopp

Dec 30 '15 at 1:31

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值