Last active
August 29, 2015 13:57
-
-
Save showsky/9482301 to your computer and use it in GitHub Desktop.
Parse Resource ID in the export Android jar
This file contains hidden or 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 class LibraryActivity extends Activity { | |
| private String msg = "hello"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(ParseResourceID.getIdByName(getApplication(), "layout", "activity_main")); | |
| TextView mTextView = (TextView) findViewById(ParseResourceID.getIdByName(getApplication(), "id", "textView1")); | |
| mTextView.setText(msg); | |
| Button mButton = (Button) findViewById(ParseResourceID.getIdByName(getApplication(), "id", "button1")); | |
| mButton.setText(msg); | |
| mButton.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } |
This file contains hidden or 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 class ParseResourceID { | |
| public static int getIdByName(Context context, String className, String name) { | |
| String packageName = context.getPackageName(); | |
| 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; | |
| } | |
| public static int[] getIdsByName(Context context, String className, String name) { | |
| String packageName = context.getPackageName(); | |
| Class r = null; | |
| int[] ids = null; | |
| 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) && (desireClass.getField(name).get(desireClass) != null) && | |
| (desireClass.getField(name).get(desireClass).getClass().isArray())) | |
| ids = (int[])desireClass.getField(name).get(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 ids; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment