As explained in POST by @hasanghaforian,
You can get the resource identifier using getIdentifier "with the following code:
int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
OR
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
Where
bug.png is the file in " /res/drawable/ ".
Then you can use layout.setBackgroundResource(resID) .
source share