Sunday, February 28, 2016

Android getting resource ID by name

Sometimes, it becomes necessary to have resource ID by name of the image. Below code works great for doing this

try {
        Class res = R.drawable.class;
        Field field = res.getField("drawableName");
        int drawableId = field.getInt(null);
    }
    catch (Exception e) {
        Log.e("MyTag", "Failure to get drawable id.", e);

    }

references:
http://stackoverflow.com/questions/3042961/how-do-i-get-the-resource-id-of-an-image-if-i-know-its-name

No comments:

Post a Comment