ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


Titanium Android Module

a Simple Prototype for Create Titanium Android Native Module.
In this Prototype I will create a Titanium Android Native Module calling "testmodule"
And Create Four simple Maths calculation

Java

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package com.testmodule;

import org.appcelerator.kroll.KrollModule;  
import org.appcelerator.kroll.annotations.Kroll;

import org.appcelerator.titanium.TiApplication;  
import org.appcelerator.kroll.common.Log;  
import org.appcelerator.kroll.common.TiConfig;


@Kroll.module(name="Testmodule", id="com.testmodule")
public class TestmoduleModule extends KrollModule  
{

    // Standard Debugging variables
    private static final String LCAT = "TestmoduleModule";
    private static final boolean DBG = TiConfig.LOGD;

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;

    public TestmoduleModule()
    {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app)
    {
        Log.d(LCAT, "inside onAppCreate");
        // put module init code that needs to run when the application is created
    }

    // Methods
    @Kroll.method
    public String example()
    {
        Log.d(LCAT, "example called");
        return "hello world";
    }   

    @Kroll.method
    public int Add(int value , int value2)
    {
        Log.d(LCAT, "The Design Team");
        return value + value2;
    }

    @Kroll.method
    public int Subtract(int value , int value2)
    {
        Log.d(LCAT, "The Design Team");
        return value - value2;
    }

    @Kroll.method
    public int Multiply(int value , int value2)
    {
        Log.d(LCAT, "The Design Team");
        return value * value2;
    }

    @Kroll.method
    public int Divide(int value , int value2)
    {
        Log.d(LCAT, "The Design Team");
        return value / value2;
    }

    // Properties
    @Kroll.getProperty
    public String getExampleProp()
    {
        Log.d(LCAT, "get example property");
        return "Time to Go";
    }


    @Kroll.setProperty
    public void setExampleProp(String value) {
        Log.d(LCAT, "set example property: " + value);
    }

}

JavaScript

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

// create tab group
var tabGroup = Titanium.UI.createTabGroup();

var test = require('com.testmodule');

Ti.API.info("module exampleProp is => " + test.exampleProp);

Ti.API.info("Add = "+test.Add(10,20));  
Ti.API.info("Subtract = "+test.Subtract(30,20));  
Ti.API.info("Multiply = "+test.Multiply(10,20));  
Ti.API.info("Divide = "+test.Divide(100,20));


//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Tab 1',
    window:win1
});

var label1 = Titanium.UI.createLabel({  
    color:'#999',
    text: "Testing",
    font:{fontSize:20,fontFamily:'Helvetica Neue'},
    textAlign:'center',
    width:'auto'
});

win1.add(label1);

tabGroup.addTab(tab1);  

tabGroup.open();

Result