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.
How to get Resource Name from Resource id
In your Activity, try these:
to get string like
radio1
:getResources().getResourceEntryName(int resid);
to get string like
com.sample.app:id/radio1
:getResources().getResourceName(int resid);
In Kotlin Now :
val name = v.context.resources.getResourceEntryName(v.id)
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 resource Id from String value(not name)
There's no method to lookup by value as it is not guaranteed to be unique. You need to iterate over all i.e. strings and compare values yourself.
Android, getting resource ID from string?
@EboMike: I didn't know that Resources.getIdentifier()
existed.
In my projects I used the following code to do that:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
It would be used like this for getting the value of R.drawable.icon
resource integer value
int resID = getResId("icon", R.drawable.class); // or other resource class
I just found a blog post saying that Resources.getIdentifier()
is slower than using reflection like I did. Check it out.
Related Topics
Multiple Scanner Inputs (Java)
How to Scroll to the Bottom of a Recyclerview? Scrolltoposition Doesn't Work
How to Get the Parent Element Name of a Child Element in Json
How to Link Feature and Step Definition in Cucumber
Reading a Text File Character by Character into a 2D Array in Java
Setting a Log File Name to Include Current Date in Log4J
Spring Boot Not Recognizing Application.Properties File
How to Resolve Uri Encoding Problem in Spring-Boot
How to Parse a Json Input Stream
How to Get the Path of Src/Test/Resources Directory in Junit
Replacing Double Backslashes With Single Backslash
How to Find the Number of Times If or Else Code Has Executed
Failed to Process Import Candidates for Configuration Class
Map Yaml to Object Hashmap in Springboot