tos tos - 3 months ago 25
Android Question

Accessing application resources from the library project

My application depends on a library project.

The menu.xml file is within the application project.

All the java code is within the library project, including the menu handler code onOptionsItemSelected().

Is there a way to access the application resources from library project ? I'd like to write something like this, which is currently impossible, since menu items are not visible from the library:

if ( item.getItemId()==R.id.settings ) {
...
}

Answer

Yes you can if you know the package name of your library.
Resources.getIdentifier(...)

You can do:

getResources().getIdentifier("res_name", "res_type", "com.library.package");

ex:

R.id.settings would be:

getResources().getIdentifier("settings", "id", "com.library.package");

Comments