This is the follow up post from Android Module Development series. Today I am going to continue with next topic which is
Accessing module and app resources.
- Understanding methods, properties, constants and module life cycle
- Using event listeners and callbacks in Module
- Converting native control into Titanium view
- Accessing module and app resources
- Using XML layouts in modules
In Android module we can able to access resources from both module and application.
1. Accessing resource from Module
Let assume that you have few image resource in your module directory and if you want to access them, you can do that in following way
//creating new button
Button moduleButton = new Button(proxy.getActivity());
moduleButton.setText("Image from module");
moduleButton.setTextSize(20);
//since we can't access R.java in Titanium module, we are getting the
//resource id using packageName and resource type
String packageName = proxy.getActivity().getPackageName();
Resources resources = proxy.getActivity().getResources();
//getIdentifier method will return the resource id
int textStyle = resources.getIdentifier("facebook_loginbutton_blue", "drawable", packageName);
moduleButton.setBackgroundResource(textStyle);
2. Accessing resource from Application
For example if you want to pass an image resource from your application to module, you can do that in following way
//creating new image button
ImageButton appButton = new ImageButton(proxy.getActivity());
//getting the blob object of the application image
TiBlob imgObj = loadImageFromApplication(imageUrl);
//creating the bitmap from the blob object
TiDrawableReference ref = TiDrawableReference.fromBlob(proxy.getActivity(), imgObj);
appButton.setImageBitmap(ref.getBitmap());
Here
loadImageFromApplication method takes input as image file path and it convert the application image into blob object.
public TiBlob loadImageFromApplication(String imageName) {
TiBlob result = null;
try {
// Load the image from the application assets
String url = getPathToApplicationAsset(imageName);
TiBaseFile file = TiFileFactory.createTitaniumFile(
new String[] { url }, false);
Bitmap bitmap = TiUIHelper.createBitmap(file.getInputStream());
// The bitmap must be converted to a TiBlob before returning
result = TiBlob.blobFromImage(bitmap);
} catch (IOException e) {
Log.e(TAG, " EXCEPTION - IO");
}
return result;
}
Here
getPathToApplicationAsset method takes input as image file path and it locates the resource relative to the application resources folder. It return the application asset url.
private String getPathToApplicationAsset(String assetName) {
// The url for an application asset can be created by resolving the specified
// path with the proxy context. This locates a resource relative to the
// application resources folder
String result = resolveUrl(null, assetName);
return result;
}
In Javascript layer
var demo3 = require('titutorial.demo3');
var proxy = demo3.createExample({
imageUrl : "icon.png"
});
win.add(proxy);
Source Code:
You can download entire module source code here
https://github.com/TiTutorial/demo-android-module-3
Continue with part 5