Created
September 23, 2013 12:37
-
-
Save SherifMakhlouf/6669835 to your computer and use it in GitHub Desktop.
Loads Resource with out using generated R, important to library projects that has resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int getResourseIdByName(String packageName, String className, String name) { | |
Class r = null; | |
int id = 0; | |
try { | |
r = Class.forName(packageName + ".R"); | |
Class[] classes = r.getClasses(); | |
Class desireClass = null; | |
for (int i = 0; i < classes.length; i++) { | |
if (classes[i].getName().split("\\$")[1].equals(className)) { | |
desireClass = classes[i]; | |
break; | |
} | |
} | |
if (desireClass != null) { | |
id = desireClass.getField(name).getInt(desireClass); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (SecurityException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} | |
return id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment