Tuesday, 20 August 2013

Getting resources dynamically from Android

The resources in Android saved in res folder could be obtained statically or dynamically by code. To do it dynamically, the method getIdentifier of the class Resources could be used. It accepts three parameters:
- name: The name of the identifier in the res folder
- defType: The default resource type to find. For example, "string" or "drawable".
- defPackage: The default package to find.

Here is an example:
String appName = "app_name";
int appNameId = getResources().getIdentifier(appName, "string", getApplicationInfo().packageName);
String appNameString = getResources().getString(appNameId);

Source code:

Android developer - Resources
StackOverflow - Dynamic Resource Loading Android

No comments:

Post a Comment