Dynamically get drawables by ID

Solution 1:

Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.

For example:

int drawableId=getResources().getIdentifier("foo"+index, "drawable", getPackageName());

would return the value of R.drawable.fooN, where N is the number given by index.

For more, see this and this and this.