Ad

Thursday, June 12, 2014

Get resource id from name in Android

public static int getResId(String variableName, Class<?> c) {

        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }



getResId(disobj.getName(), R.drawable.class);

No comments:

Post a Comment