How to Access the Drawable Resources by Name in Android

how to access the drawable resources by name in 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);

How do you obtain a Drawable object from a resource id in android package?


Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(d);

Trying to get drawable resource ID from its name in Android

One simple solution would be to parse the string and get only the image name as below.

String[] Image1 = array[1][1].split("/");
String s = Image1[Image1.length-1].replace(".png","").trim();

int resID = this.context.getResources().getIdentifier(s , "drawable", this.context.getPackageName());

How to get drawable resource id from string name base on current app theme in android (dark/light theme)

Short answer: Don´t use setImageResource(Int id). Get the themed drawable with getResources().getDrawable(Int id, Resources.Theme theme) and use setImageDrawable(Drawable drawable) instead.

Explanation: Themed resources don´t have a particular id suffix/preffix; is the same ID across all themes, to avoid breaking the app. The theme is supposed to be applied beforehand, allowing all the theme-sensitive values to be replaced by the theme´s version. Usually, a visual context (activity) contains the reference, but depending on how/when you are trying to get the resource, you may get one with a different theming (application context for example will not have theme references), and because that, we have getDrawable(Int id, Resources.Theme theme) since api 22. That´s the root of your problem. When you call setImageDrawable(Int res), the imageView executes this block:

private void resolveUri() {
if (mDrawable != null) {
return;
}
if (getResources() == null) {
return;
}
Drawable d = null;
if (mResource != 0) {
try {
d = mContext.getDrawable(mResource);
} catch (Exception e) {
Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
// Don't try again.
mResource = 0;
}
} else if (mUri != null) {
d = getDrawableFromUri(mUri);
if (d == null) {
Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri);
// Don't try again.
mUri = null;
}
} else {
return;
}
updateDrawable(d);
}

as you can see, it uses the deprecated Context.getDrawable(Int id) method. So the drawable will, at best, match the theme reference in the context, if any. In your widget, the call likely happens before the rest of the change happens. So, specify the desired theme instead and you´ll be okay.

How can I get drawable resource by string?

You can use LocalContext to get current context, and then use same methods as you used in view based Android:

val context = LocalContext.current
val drawableId = remember(name) {
context.resources.getIdentifier(
name,
"drawable",
context.packageName
)
}
Image(
painterResource(id = drawableId),
contentDescription = "..."
)

How to get a resource id with a known resource name?

It will be something like:

R.drawable.resourcename

Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if that's what you're using).

If that doesn't work, you can always use a context's getResources method ...

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);

Where this.context is intialised as an Activity, Service or any other Context subclass.

Update:

If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?

Update 2:

The Resources class has this method:

public int getIdentifier (String name, String defType, String defPackage) 

Which returns the integer of the specified resource name, type & package.

Get drawable by string

Use this function to get a drawable when you have the image name. Note that the image name does not include the file extension.

public static Drawable GetImage(Context c, String ImageName) {
return c.getResources().getDrawable(c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName()));
}

then just use setBackgroundDrawable method.

If you only want the ID, leave out the getDrawable part
i.e.

return c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName());

Accessing drawable file from String Arraylist in android

You can get drawable from your drawable res folder by the saved name. For example if you have ic_home.png in your res/drawable folder you can get that passing "ic_home" as the first parameter of this method.

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

How to get drawable name from imageView

Change your code by converting your int ids into String names by Using the method

getResourceEntryName(int id);

Change the line:

writeID.setText(String.valueOf(generator()));

To Something like:

writeID.setText(getResources().getResourceEntryName(generator()));

And for Viceversa:

And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:

int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());

And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName().
I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.



Related Topics



Leave a reply



Submit