[Solved] Using a variable to define a resource


There are no dynamic variables in Java. Java variables have to be declared in the source code. So a solution could be to make two arrays of integers for both drawable resources and view id’s

For example

int[] resourceIds = new int[] { R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 }

int[] viewIds = new int[] { R.id.d6_image_view_1, R.id.d6_image_view_2, R.id.d6_image_view_3, R.id.d6_image_view_4, R.id.d6_image_view_5, R.id.d6_image_view_6 }

Then you could do

int image = 2;
int picture = 4;
ImageView imgView = (ImageView) findViewById(viewIds[image]);
imgView.setImageResource(resourceIds[picture]);

solved Using a variable to define a resource