How to add and retrieve a resource (string) in a Cordova plugin for Android

How to add and retrieve a resource (string) in a Cordova plugin for Android

In Android, resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. If you think about it, maybe the usage of native resources doesn't make sense as we are using an hybrid technology. Everything should be theoretically handled with Javascript isn't ? Well, there are as always in the development exceptions.

Take as example the usage of a native third party library that allow you to customize a label. With Java, the method to customize the label doesn't expect a string as value, instead it expects a number (@StringRes int stringRes), thing that makes the customization with a string from javascript impossible. Therefore you must resort to the native resources, practice that cordova allows. In this article you will learn how to add a custom resources file for android in your Cordova plugin.

1. Create your resources file

In this example, we are going to create a new xml file in the Android folder of your plugin (yourplugin/src/android) with the name "StringsFoo.xml", this will have a string resource identified with the name welcome_message:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome_message">Hello, welcome</string>
</resources>

2. Register your resources file

Now, modify the plugin.xml file in your plugin and add a new resource file with the <resource-file> tag (note that the path to the file can vary according to your needs):

Note

Do not give as name of the file strings.xml as this file already exists in any project and if it's overwritten, the cordova project won't work as it contains some important values as the app name.

<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" 
        id="com.blablabla.bla"
        version="1.0.0">
  <name>Your Plugin Name</name>
 

  <!-- Add a new resource file from the plugin to the project 
        In your plugin development, its located in /yourplugin/src/android/StringsFoo.xml
        and it will be added to the res/values folder of your Android Application
    -->
  <platform name="android">
    <resource-file src="src/android/StringsFoo.xml" target="res/values/StringsFoo.xml" />
  </platform>
</plugin>

Theoretically, once you install the plugin in a Cordova project, it will succesfully add the new resource to the project.

3. Retrieve resource value in Java

As you may know (maybe not) at build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them (if you use third party libraries, they allow you to customize some values like labels but they expect the ID as argument, not the string itself). A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.

The algorithm to retrieve the ID of a resource is to use the activity, from the activity retrieve the resources and then use the getIdentifier method that expects as first argument the name of the resource, as second the type (in this case string) and as third argument the package name of the application. That would be in your main class of the plugin that extends CordovaPlugin like:

// The name of the string resource
String name = "welcome_message";

// Retrieve the identifier (in this case the number is 2131165238 that obviously changes in your project)
int identifier = cordova.getActivity().getResources().getIdentifier(name, "string", cordova.getActivity().getPackageName());

However, if you're not working with a third party library that requires the ID of your resource, probably this number is not useful for you, but the string. To retrieve the string resource with name "welcome_message" convert the identifier to string using the following code:

String name = "welcome_message";

// Hello, welcome
String resource_text = cordova.getActivity().getString(cordova.getActivity().getResources().getIdentifier( name, "string", cordova.getActivity().getPackageName()));

From the activity use the getString method that expects as first argument the identifier of the resource. By changing the type (second parameter of the getIdentifier method) you can retrieve another type of resources like bool, layout etc but don't forget neither to change the getter method to the correct type (if you get a boolean, use getBoolean instead of getString).

Note

If you are working with cordova, but you're working in another activity (some activity that doesn't extend cordova) then you can use the same code but replace the cordova.getActivity with the activity that you're working with.

Happy coding !

Become a more social person