Thursday, October 24, 2013

Android Module Development - Part 4

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.
  1. Understanding methods, properties, constants and module life cycle
  2. Using event listeners and callbacks in Module
  3. Converting native control into Titanium view
  4. Accessing module and app resources
  5. 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");  

//since we can't access 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);

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);
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"
Source Code:
You can download entire module source code here

Continue with part 5 


  1. it looks like you can read my mind :) for the last two days I was struggeling to get my image from my js application to my module (imagebutton) but couldn't find a way to do this (except as a blob). Thanks for this tutorial! Hard to find any other documentation for module development