Skip to content

Instantly share code, notes, and snippets.

@marshall
Created February 22, 2011 17:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save marshall/839017 to your computer and use it in GitHub Desktop.
Save marshall/839017 to your computer and use it in GitHub Desktop.
Native Titanium + Android example code
var activity = Ti.Android.currentActivity;
var win = Ti.UI.currentWindow;
activity.addEventListener("create", function(e) {
var button = Ti.UI.createButton({title: "Hello world"});
button.addEventListener("click", function(e) {
activity.finish();
});
win.add(button);
});
var win = Ti.UI.createWindow({
backgroundColor: 'white', layout: 'vertical'
});
var launchButton = Ti.UI.createButton({
top: 10, width: 200, title: "Launch JS Activity"
});
launchButton.addEventListener("click", function(e) {
var intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_MAIN,
url: 'activity1.js'
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
Ti.Android.currentActivity.startActivity(intent);
});
win.add(launchButton);
var qrScanButton = Ti.UI.createButton({ title: "Scan QR Code", width: 200 });
qrScanButton.addEventListener("click", function(e) {
var intent = Ti.Android.createIntent({
action: "com.google.zxing.client.android.SCAN"
});
intent.putExtra("SCAN_MODE", "QR_SCAN_MODE");
var activity = Ti.Android.currentActivity;
activity.startActivityForResult(intent, function(e) {
if (e.resultCode == Ti.Android.RESULT_OK) {
var contents = e.intent.getStringExtra("SCAN_RESULT");
var format = e.intent.getStringExtra("SCAN_RESULT_FORMAT");
status.text = "Contents: " + contents + ", Format: " + format;
} else if (e.resultCode == Ti.Android.RESULT_CANCELED) {
status.text = "Scan Canceled!";
}
});
});
win.add(qrScanButton);
var dialPhoneButton = Ti.UI.createButton({ title: "Dial 214-555-1234", width: 200 });
dialPhoneButton.addEventListener("click", function(e) {
var intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_DIAL,
data: "tel:2145551234"
});
Ti.Android.currentActivity.startActivity(intent);
});
win.add(dialPhoneButton);
var shareText = Ti.UI.createTextArea({ left: 10, right: 10, height: 150 });
win.add(shareText);
var shareButton = Ti.UI.createButton({ left: 10, right: 10, title: "Share" });
shareButton.addEventListener("click", function(e) {
var intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_SEND,
type: "text/plain",
});
intent.putExtra(Ti.Android.EXTRA_TEXT, shareText.value);
Ti.Android.currentActivity.startActivity(
Ti.Android.createIntentChooser(intent, "Pick something"));
});
win.add(shareButton);
var status = Ti.UI.createLabel({
left: 10,
font: {
fontSize: 24,
fontWeight: 'bold'
},
text: "Status"
});
win.add(status);
win.open();
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<id>com.appcelerator.nativeti</id>
<name>nativeTi</name>
<version>1.0</version>
<publisher>not specified</publisher>
<url>not specified</url>
<description>not specified</description>
<copyright>not specified</copyright>
<icon>appicon.png</icon>
<persistent-wifi>false</persistent-wifi>
<prerendered-icon>false</prerendered-icon>
<statusbar-style>default</statusbar-style>
<statusbar-hidden>false</statusbar-hidden>
<fullscreen>false</fullscreen>
<navbar-hidden>false</navbar-hidden>
<analytics>true</analytics>
<guid>7c759e42-aab6-4d9e-bf40-428be9cec903</guid>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<activities>
<activity url="activity1.js"/>
</activities>
</android>
<modules>
</modules>
</ti:app>
@dawsontoth
Copy link

I'm going to reiterate how much I appreciate this gist. Helped me figure out a couple things about integrating js and activities!

@anfaguerrero
Copy link

if i have in folder Resources/UI/file.js
when i declare activity in tiap.xml, have to write whole url. like ?
thanks

@olman21
Copy link

olman21 commented Dec 19, 2014

I'm using alloy, where should be located the "activity1.js" file?

@falkolab
Copy link

@olman21 Not actual fo you but ... you can place it to the app/lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment