I plan to write series of blog posts about Android module development in Titanium. I wish to cover below topics in each week with clear example
Environment Setup
Creating a sample Android Module
You can create a module using either CLI or Titanium Studio, Creating New Titanium Module document explains more about this.
Okay, cool. Now you have setup the module development environment and created a module. Now you can see following files inside yours module’s res folder(module_root/res). Let assume that your module name is demo
Methods
Inside module you can declare method by preceding with @Kroll.method annotation.
In Module
2. Using propertyAccessors Annotation
You can also define a set of properties in your @Kroll.proxy or @Kroll.method annotation using the propertyAccessors element, and the accessors will be automatically generated for you:
Constants are declared with @Kroll.constant annotation. Here's an example:
In module
Continue with part 2
- 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
Environment Setup
- Titanium SDK.
- All of the prerequisites for developing Android applications.
- Android NDK. Add an ANDROID_NDK environment variable pointing to the NDK folder.
- Ant 1.7.1 or above must be installed and assigned in your system PATH, to build from the command line.
- Eclipse and ADT can be used instead of or in addition to Ant.
- gperf must be installed and assigned in your system PATH.
Creating a sample Android Module
You can create a module using either CLI or Titanium Studio, Creating New Titanium Module document explains more about this.
Okay, cool. Now you have setup the module development environment and created a module. Now you can see following files inside yours module’s res folder(module_root/res). Let assume that your module name is demo
- DemoModule.java
- ExampleProxy.java - (this is used to return view from module)
Module Life-cycle Events
If you want to do anything during application startup, you can use onAppCreate method. This is optional method. It has @Kroll.onAppCreate annotation before its declaration
@Kroll.onAppCreate
public static void onAppCreate(TiApplication app)
{
Log.d(LCAT, "[MODULE LIFECYCLE EVENT] onAppCreate notification");
}
Methods
Inside module you can declare method by preceding with @Kroll.method annotation.
In Module
//this method print the log message
@Kroll.method
public void printLog() {
Log.d (TAG,"Hello world..." );
}
//this method concat two strings and return the result string
@Kroll.method
public String addStrings(String a, String b)
{
return a.concat(b);
}
In Javascript
//in javascript you can access the method by
var demo = require("titutorial.demo");
demo.printLog();
demo.addStrings("Hello", "World");
Properties
We can set the properties for the module in two ways,
We can set the properties for the module in two ways,
- Using custom getter and setter methods for each property
- Using propertyAccessors annotation
1. Using Custom Accessor Methods
Getter and setter methods are declared with @Kroll.getProperty and @Kroll.setProperty annotations. Here we need to specify the getter and setter methods for each property.
In module
Getter and setter methods are declared with @Kroll.getProperty and @Kroll.setProperty annotations. Here we need to specify the getter and setter methods for each property.
@Kroll.getProperty @Kroll.method
public String getTag() {
return tag;
}
@Kroll.setProperty @Kroll.method
public void setTag(String t) {
tag = t;
}
In javascript
var demo = require("titutorial.demo");
//getting property value
alert(demo.tag);// or
alert(demo.getTag());
//setting property value
demo.tag = "testing"; // or
demo.setTag("testing");
2. Using propertyAccessors Annotation
You can also define a set of properties in your @Kroll.proxy or @Kroll.method annotation using the propertyAccessors element, and the accessors will be automatically generated for you:
// hintText and value prop declared using propertyAccessors
@Kroll.module(name="Demo", id="titutorial.demo", propertyAccessors = { "hintText", "value" })
Constants:Constants are declared with @Kroll.constant annotation. Here's an example:
In module
@Kroll.module
public class DemoModule extends KrollModule {
@Kroll.constant
public static final int PAGE_LIMIT = 10;
}
In javascript
//constant can now be referred to directly from JavaScript
var demo = require("titutorial.demo");
alert(demo.PAGE_LIMIT);
Here you can find demo module source code https://github.com/TiTutorial/demo-android-moduleContinue with part 2
Nice tutorial! Started some basic modules the other day too. This comes very handy! Looking forward for the next one
ReplyDeleteThanks Michael, here you go http://www.titaniumtutorial.com/2013/10/android-module-development-part-2_9.html
ReplyDeleteGood Tutorial .......
ReplyDelete