Skip to content Skip to sidebar Skip to footer

Accessing Resources Programmatically

Is it possible to receive the resource-ids being kept by a as an int[] programmatically without referring to the resource-class R? Copy

Maybe this could help somebody one day.


Solution 2:

Slightly more efficient solution:

public static final int[] getResourceDeclareStyleableIntArray(String name) {
        Field[] allFields = R.styleable.class.getFields();
        for (Field field : allFields) {
            if (name.equals(field.getName())) {
                try {
                    return (int[]) field.get(R.styleable.class);
                } catch (IllegalAccessException ignore) {}
            }
        }

        return null;
    }

Solution 3:

public static final int[] getResourceDeclareStyleableIntArray(String name) {
    int[] result = null;
    try {
        result = (int[]) R.styleable.class.getField(name).get(R.styleable.class);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return result;
}

Post a Comment for "Accessing Resources Programmatically"