Ginso
Ginso

Reputation: 569

android: get the application name from inside a module

i'm writing an android module, which i will include in some of my projects. Now i need to get the application name(at runtime), e.g. the name that is set as android:label for the application in the manifest. Obviously i can't use getResources().getString(R.string.app_name) because i can't access the resources of the project that uses this library. Is there another way? I tried getApplicationInfo().name but that is null and i can't find anything else.

Upvotes: 6

Views: 2636

Answers (2)

M.M
M.M

Reputation: 26

The module is included in the application so I guess you are calling one of its methods in the application. If it is the case, I propose that you add a parameter in this method to get the context of the application. Then you can use context.getResources().getString(R.string.app_name) in your module to get the application name.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007296

I tried getApplicationInfo().name but that is null

That is because name comes from the android:name attribute on <application>, and you are not using a custom Application subclass in your app.

i can't find anything else.

Call loadLabel() on the ApplicationInfo, passing in a PackageManager as a parameter, to retrieve the android:label value for an <application> or any component (e.g., <activity>).

Upvotes: 6

Related Questions