Like other resources in Android, images are accessible through the "R" class, which is just a collection of static classes containing static integer fields. There is no βget all available namesβ metthod (at least I don't know) other than using reflection.
You will need a list of available identifiers for randomization. You can automatically populate this list with reflection:
import java.lang.reflect.Field; ... Field[] fields = R.drawable.class.getFields(); List<Integer> drawables = new ArrayList<Integer>(); for (Field field : fields) { // Take only those with name starting with "fr" if (field.getName().startsWith("fr")) { drawables.add(field.getInt(null)); } }
Thus, you will get a list of identifiers of available objects that interest you. You can use these identifiers later when you usually use, for example. R.drawable.someResource
source share