how to access the drawable resources by name in android

AndroidDrawable

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

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"
    ....

Solution 3 - Android

Modify image content:

    ImageView image = (ImageView)view.findViewById(R.id.imagenElement);
    int resourceImage = activity.getResources().getIdentifier(element.getImageName(), "drawable", activity.getPackageName());
    image.setImageResource(resourceImage);

Solution 4 - Android

Using Kotlin

fun Context.getResource(name:String): Drawable? {
    val resID = this.resources.getIdentifier(name , "drawable", this.packageName)
    return ActivityCompat.getDrawable(this,resID)
}

I wrote it as Extension function so It can be used at any place in code.

Note: context.getResources().getDrawable(resourceId); is deprecated in Java.

Note: name of file,is name without extensions for ex "a.png" name will be "a".

Solution 5 - Android

You can achieve like this

int resourceId = getResources().getIdentifier("your_drawable_name", "drawable", getPackageName());

Set resourceId In Imageview

imageView.setImageResource(resourceId);

Solution 6 - Android

if You need int resouce

 Resources resources = context.getResources();
 int resourceId = resources.getIdentifier("eskb048", "drawable",context.getPackageName());
 // return like: R.drawable.eskb048.png

if your need Drawable check the first answer is correct for all

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhguserView Question on Stackoverflow
Solution 1 - AndroidianhanniballakeView Answer on Stackoverflow
Solution 2 - AndroidssantosView Answer on Stackoverflow
Solution 3 - AndroidRaulView Answer on Stackoverflow
Solution 4 - AndroidMahmoud MabrokView Answer on Stackoverflow
Solution 5 - AndroidManasView Answer on Stackoverflow
Solution 6 - AndroidRahman RezaeeView Answer on Stackoverflow