Home » Android » c# – Getting Drawable Resource

c# – Getting Drawable Resource

Posted by: admin June 15, 2020 Leave a comment


I am using MonoDevelop for Android and i am trying to create a Drawable variable from an image in the resources folder.

I have the following files in my Resources\drawable folder:


How do I create a Drawable variable that has the image obtained from one of these files?

I have tried the following code:

Drawable icon = Drawable.CreateFromPath("Icon.png");

This however does not work. If this is the correct way to load an image from file, do I need to have the full filename and path to the file? I have tried many combinations to access the image, with no luck.

I have also tried the following:

Drawable icon = R.getResources().getDrawable(R.drawable.Icon); 
Drawable icon = R.getResources().getDrawable(R.drawable.monkey);

Also with no luck.

Can I please have some help in correctly creating a Drawable variable that has an image from the resource folder?


How to&Answers:

You’re very close, but I think you’re mixing Java and C#.

You can access this using the Resource structure. I’ve put both the Mono for Android and Java versions below for reference but in your case you want the C# version if you’re using Mono for Android.

Mono for Android (C#)

Drawable icon = Resources.GetDrawable(Resource.Drawable.Icon);

Android SDK (Java)

Resources res = getResources();
Drawable icon = res.getDrawable(R.drawable.Icon);


As of Android API version 21, the old method is deprecated and replaced by the following:

Drawable icon = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.Icon, null);

For more details check the original Android answer