BetaCodeShareBeta

by Code Solutions Project

Simple solutions for common problems

External library: Android SDK Version used: 23
Description: Get Android's resource by string name
Java
JoseluCross
public static int getResId(String resName, Class<?> c) {

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

Input example

"myImage",R.drawable.class

Output example

Integer of my resource
×Oh snap! Something wrong