Accessing Android Resources By Name at Runtime
Use the getIdentifier method passing it the name of your resource, the resource type (id, raw) and your package name ( just use getPackageName())
int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
After that you can use the id just like you would at compile time using the R class.
Updated: May 21, 2010 – The code that was here for a long time was awful and dated, so I’ve updated this post.