androidloopsdrawable

Get sequential file name from drawable folder


I am writing an application where using 200+ images placed in drawable folder and all these files follow same naming convention say thumb+X

To read/display these files one way is to select every drawable file by follow R.drawable.XXX.

Is there any way to read these file name using loop or something else..so no matter how many file it will update in drawable, no need to touch source file then.

Any suggestion!


Solution

  • Its Very Simple first you create an String Value Like.

    String name= "R.drawable.thumb";
    

    Now loop it like

    for(int i=1;i<=200;i++){
        String resource = name + i;
        try {
            Class c = Drawable.class;
            Field idField = c.getDeclaredField(resource);
            int resourceDrwable = idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Its Done.

    Another Way is

    int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", "android");