Tuesday, November 5, 2013

Android Module Development - Part 5

This is the follow up post from Android Module Development series. Today I am
going to continue with next topic which is Using XML layouts in modules.
  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
Usually in native Android development, we will define out user interface and layouts in xml file. Same thing we can implement in Titanium Android module also. For that we have to use LayoutInflater.

LayoutInflater class is used to instantiate layout XML file into its corresponding View objects. In other words, it takes as input an XML file and builds the View objects from it.

Lets consider our RatingBar module, here I am using xml layout file (raingbar_layout.xml) to create RatingBar with custom styles. In module I can access the xml layout elements in following way
View raingBarWrapper;
int resId_raingBarHolder = -1, resId_ratingBar = -1;

//fetching app package name and resources 
String packageName = proxy.getActivity().getPackageName();
Resources resources = proxy.getActivity().getResources();

//fetching resource id
resId_raingBarHolder = resources.getIdentifier("raingbar_layout", "layout", packageName);
resId_ratingBar = resources.getIdentifier("ratingbar_default","id", packageName);

LayoutInflater inflater = LayoutInflater.from(getActivity());
//inflating "raingbar_layout" xml file
raingBarWrapper = inflater.inflate(resId_raingBarHolder, null);

//getting reference to RatingBar component in layout
ratingBar = (RatingBar) raingBarWrapper.findViewById(resId_ratingBar);

//adding properties to RatingBar component

XML layout file (raingbar_layout.xml)


Source Code:
You can download entire RatingBar module source code here


  1. These tutorials are great! Have you ever though about exposing Android's Drawer Layout as a Titanium Module? (

  2. I don't have any idea about converting Android's Drawer layout into a Titanium module. It has more challenges. Meantime you can do similar navigation using with TiDraggable module( ).

  3. Yep I got it work.

    One more issue I am facing is to refer external jar file after adding this I am unable to build module.

  4. I am trying to user DrawerLayout for this I have added android-support-v4.jar in modules lib folder and then added it in library via Java Build Path.By this modules gets generated but while using it in app it say

    [ERROR] java.lang.IllegalArgumentException: already added:

    What to do with this?

  5. Also I did one hack to delete jar from compiled module lib folder after that It say
    Sending event: exception on thread: main msg:java.lang.VerifyError:

  6. Thanks! these posts show the magic of Titanium.
    Is is possible to pack a pure native android activity(without layout xml) into a module? Or to repack the apt file into a Titanium module and launch the native activity from JS? and how about so library?