Android, getting resource ID from string?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To get a resource ID from a string in Android, use getResources().getIdentifier("resource_name", "resource_type", getPackageName()). This method resolves a resource name at runtime and returns its integer ID, or 0 if the resource does not exist. It is the standard approach when you cannot reference a resource through the compile-time R class.
This capability matters in real projects. Dynamic theming, plugin architectures, server-driven UIs, and internationalization workflows all need to resolve resource names that are not known until runtime. Understanding how the Android resource system works under the hood helps you use getIdentifier() effectively and avoid the performance traps that come with it.
How the Android Resource System Works
Android resources live in the res/ directory, organized into typed subdirectories: drawable/, layout/, values/, mipmap/, and others. At build time, the Android Asset Packaging Tool (AAPT2) assigns each resource a unique 32-bit integer ID and writes these IDs into the auto-generated R class.
When you write R.string.app_name in your code, the compiler replaces it with the integer constant directly. This is fast because there is no lookup at runtime. The getIdentifier() method exists for the cases where you cannot use this compile-time path.
Using getIdentifier() to Resolve Resource Names
The Resources.getIdentifier() method accepts three parameters and returns the resource ID as an integer.
The method returns 0 when no matching resource is found. Always check for this before using the ID.
Complete Example in an Activity
Kotlin Equivalent
Accessing Resources from Other Packages
When you need a resource from a different package (for example, a system resource), pass that package name as the third argument.
Practical Use Cases
Dynamic Theming
When themes are defined as resource sets and the active theme is selected at runtime (perhaps from a remote config), you can resolve color or style resources by name.
Server-Driven UI
A backend API can send layout element names that the app resolves locally. This avoids shipping image binaries over the network while still letting the server control which assets appear.
Iterating Over Numbered Resources
When resources follow a naming convention like level_1, level_2, etc., you can load them in a loop without listing every ID manually.
Performance Comparison
| Approach | Lookup Cost | Use When |
R.drawable.icon (compile-time) | Zero runtime cost, compiled to constant | Resource name is known at compile time |
getIdentifier() (runtime) | String-based lookup through resource table | Resource name determined at runtime |
Cached getIdentifier() result | One-time lookup cost, then constant access | Same dynamic resource accessed repeatedly |
The runtime lookup is measurably slower than a direct R constant. On most devices the difference per call is small (microseconds), but it adds up in tight loops, RecyclerView adapters, or animation frames. Cache the result when you use the same resource name more than once.
Common Pitfalls
- Not checking for zero.
getIdentifier()returns0when the resource is not found. Passing0to methods likegetString()orsetImageResource()throws aResources.NotFoundExceptionat runtime. - Wrong resource type string. Passing
"Drawable"instead of"drawable"or"str"instead of"string"silently returns0. The type must match the subdirectory name exactly, in lowercase. - Using
getIdentifier()in a loop without caching. Each call traverses the resource table. In aRecyclerView.Adapter.onBindViewHolder(), this can cause visible jank. Resolve the IDs once and store them in a map. - Relying on
getIdentifier()for resources that exist at compile time. If you know the resource name when writing the code, use theRclass directly. The compiler can then verify the resource exists, and ProGuard/R8 can optimize the reference. - Forgetting resource name changes after refactoring. Because
getIdentifier()uses a string, the compiler cannot warn you when a resource is renamed or deleted. This creates silent failures that only surface at runtime. - Confusing package name with application ID. In build variants where
applicationIddiffers from the Java package, pass the correct application ID. UsinggetPackageName()from an Activity or Context returns the right value, but hard-coding a package string can break in flavor builds.
Summary
- Use
getResources().getIdentifier(name, type, package)to resolve a resource ID from a string at runtime. - The method returns
0when the resource is not found. Always guard against this. - Prefer compile-time
Rclass references whenever possible. They are faster, type-safe, and verified by the compiler. - Cache the result of
getIdentifier()when you need the same resource ID more than once. - Common use cases include dynamic theming, server-driven UIs, plugin architectures, and iterating over conventionally named resources.
- Watch for case-sensitive type strings, stale resource names after refactoring, and the difference between package name and application ID in multi-flavor builds.

