Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access the drawable resources by name in android

In my application, I need to get the some bitmap drawables somewhere where I do not want to keep the reference R. So I create a class DrawableManager to manage the drawables.

public class DrawableManager {     private static Context context = null;      public static void init(Context c) {         context = c;     }      public static Drawable getDrawable(String name) {         return R.drawable.?     } } 

Then I want to get the drawable by name somewhere like this( the car.png is put inside the res/drawables):

Drawable d= DrawableManager.getDrawable("car.png"); 

However as you can see, I can not access the resources by the name:

public static Drawable getDrawable(String name) {     return R.drawable.? } 

Any alternatives?

like image 457
hguser Avatar asked May 04 '13 01:05

hguser


People also ask

How do you get a drawable resource from a name?

public class DrawableManager { private static Context context = null; public static void init(Context c) { context = c; } public static Drawable getDrawable(String name) { return R. drawable.? } }

How do you call drawable on android?

But Best way to do is :R. drawable. ic_delete); OR use the below code for Drawable left, top, right, bottom.

What are drawable resources in android?

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.


2 Answers

Note that your approach is almost always the wrong way to do things (better to pass the context into the object itself that is using the drawable than keeping a static Context somewhere).

Given that, if you want to do dynamic drawable loading, you can use getIdentifier:

Resources resources = context.getResources(); final int resourceId = resources.getIdentifier(name, "drawable",     context.getPackageName()); return resources.getDrawable(resourceId); 
like image 158
ianhanniballake Avatar answered Nov 03 '22 20:11

ianhanniballake


You could do something like this.-

public static Drawable getDrawable(String name) {     Context context = YourApplication.getContext();     int resourceId = context.getResources().getIdentifier(name, "drawable", YourApplication.getContext().getPackageName());     return context.getResources().getDrawable(resourceId); } 

In order to access the context from anywhere, you may extend Application class.-

public class YourApplication extends Application {      private static YourApplication instance;      public YourApplication() {         instance = this;     }      public static Context getContext() {         return instance;     } } 

And map it in your Manifest application tag

<application     android:name=".YourApplication"     .... 
like image 38
ssantos Avatar answered Nov 03 '22 21:11

ssantos