Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Drawable Resource

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:

Icon.png
monkey.png
monkeyPurple.png

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?

Thanks

like image 497
Garry Avatar asked Nov 22 '12 01:11

Garry


People also ask

How do you get a resource image?

You can use imageButton. getDrawable() to get the drawable of the ImageButton object and then use setImageDrawable() instead of setImageResource() .

What are drawable resources?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.


1 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);
like image 137
Kirk Avatar answered Oct 02 '22 13:10

Kirk