Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing application resources from the library project

Tags:

android

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 ) {
...
}
like image 922
tos Avatar asked Feb 23 '12 21:02

tos


People also ask

What is application resources?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.

How do I access library on Android?

You can find your History, Watch later, Playlists, and other channel details in your Library. To find your Library, go to the bottom menu bar and select Library .

What is library project in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.


2 Answers

Yes you can if you know the package name of your library. See: 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");
like image 191
triad Avatar answered Sep 20 '22 17:09

triad


You should really just include a version of the menu.xml resource in your library project. If you want to have a different menu.xml in your application, you can do that and it will override the copy from the library project.

From the Library Projects docs:

In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.

like image 29
kabuko Avatar answered Sep 17 '22 17:09

kabuko