Android - how can I dynamically load drawables (pictures)? Names not known until run-time -
i've searched google , website many times, can not find solution question.
first, understand, can load images using like:
int image = r.drawable.icon; (assuming there's file called 'icon.png')
and i've read about, , tried using:
getresources().getidentifier("resname", "restype", com.example.whatever);
but, here's i'm unable do: want able have pictures in /res/drawable folder (i believe that's correct folder), without knowing of names of files, , load them dynamically - @ run-time.
one (of many) things i've tried (something like):
int[] images = new int[numberofimages]; for( 0 numberofimages ) { images[i] = getresources().getidentifier("resname", "restype", com.example.whatever); }
this returns 0 every time, it's not going work
i'd name , integer identifier every picture in /res/drawables folder. can done without knowing file names?
----------------------------------------------------------------------
[adding after question resolved may run same issue in future. shows in fact work.]
class resources = r.drawable.class; field[] fields = resources.getfields(); string[] imagename = new string[fields.length]; int index = 0; for( field field : fields ) { imagename[index] = field.getname(); index++; } int result = getresources().getidentifier(imagename[10], "drawable", "com.example.name");
well, if have it, use reflection :)
class resources = r.drawable.class; field[] fields = resources.getfields(); (field field : fields) { system.out.println(field.getname()); }
however, in way, still hard-coding stuff, since putting fixed set of drawables in app. :)
Comments
Post a Comment