Category Archives: Titanium

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

Open a webpage in Titanium (iPhone or ANDROID).

Opening a webpage in Titanium is really simple. Look at the following sample.

function openWebpage(){
	var URL = "http://www.google.com"	;
	Titanium.Platform.openURL(URL);
};

If you are running in iPhone then it will open safari and if it is ANDROID, it will open the default browser with the supplied URL String.
Happy coding……..

Please leave your valuable comments.

Get a file from PhotoGallery and copy it to your directory in your project resources in Titanium(iPhone or ANDROID).

This example opens the photogallery and then when you select a file from it , it will be copied to your resources directory.
First manually create a directory in your resources, here the directory is named “mydirectory”.
Call this functiion inside a button click or something

var directory = "mydirectory"
 Titanium.Media.openPhotoGallery({
	success:function(event)
	{
		var cropRect = event.cropRect;
		var image = event.media;
		var mime_type = image.mimeType;  	// Getting the file type.....
		var arr = Array();
		arr = mime_type.split('/');
		var image_type = arr[1];
		if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
		{
			var image_name = "My_img""."+arr[1];
			Ti.API.info(image_name);
			var filename = Titanium.Filesystem.resourcesDirectory +  directory + image_name;
			var bgImage = Titanium.Filesystem.getFile(filename);
			bgImage.write(image);                  	//write the image binary to new image file.
	},
	cancel:function()
	{	Ti.API.info(' Cancelled ');		},
	error:function(error)
	{  	Ti.API.info(' An error occurred!! ');	 	},
	allowEditing:true,
	mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
});

Now after running this code check the directory you have created.

If you want to delete the image use this

var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+"/"+directory +"/"+My_img.png);
file.deleteFile();

Note: Change it to appropriate file extension.

If you want to list all the files inside the directory use this code…

var my_dir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+"/"+directory+"/");
		var files = my_dir.getDirectoryListing().toString();
		Ti.API.info('directoryListing = ' + files);
		var files_array = Array();
		files_array = gal_files.split(',');
		var num_of_files = files_array.length;

If you want to delete a directory Use this

	var my_dir= Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+"/"+directory+"/");
		 	if(my_dir.exists()){
				var files = gal_dir.getDirectoryListing().toString();
				var files_array = Array();
				files_array = gal_files1.split(',');
				var num_of_files = gal_files_array1.length;

				for(var j = 0; j < num_of_files ; j++){
					var file1 = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+"/"+directory+"/"+files_array[j]);
					file1.deleteFile();
				}
				my_dir.deleteDirectory();
		 	}

Please leave your valuable comments.

How to use tableView in Titanium, iPhone or ANDROID?

This tutorial shows how to create a tableview in Titanium and add data to it.
How to handle a click function in the tableView and get the data from the row.


   /* Create a new window */
   var win = Titanium.UI.createWindow({
			backgroundColor:'#336699',
			backgroundImage:'images/bkg.png',
			navBarHidden:true,
			tabBarHidden:true
	});

	/* Datasource for the tableView */
    var my_data = [
		{title:'data1'},
		{title:'data2'},
		{title:'data3'},
		{title:'data4'},
		{title:'data5'}
	];

    var tableview = Titanium.UI.createTableView({
		data:my_data,
		top:0
	});

	/* Alternately you can use this to set the datasource in the tableView.
	tableview.setData(my_data);
	*/

	/* this will handle the click function in the tableView */
	tableview.addEventListener('click', function(e)
	{
		e.rowData.hasCheck = true;		 // add a checkmark to the row.
		var title = e.rowData.title;	       // get the title property of the clicked row.
                var index = e.index;                     // got the clicked index;
                Ti.API.info("Clicked on index : " + index + "with row title : " + title);
	});

	win.add(tableview); 				// add the tableview to the window.

	win.open();							// open the window.

Please leave your valuable comments.

How to place a label or textBox or anyother control inside a tableView in Titanium, ANDROID or iPhone ?

Hi all……..
This tutorial is for Titanium users. This tutorial explains how to place a textBox or anyother control inside a tableView.
A tableview requires data in the form of a array, so we create an array (here array is named “data_array“) by adding the controls like textboxes and labels inside it. You can give a large number of properties to customize the controls. i have given some of them.

                var data_array= [];  // This is the array that we are going to give to the tableView.

                /* creating a textField to place in the tableView */
                var name_tf = Titanium.UI.createTextField({
			color:'#336699',
			hintText: 'Enter Name',
			height:45,
			value: "",
			left:100,right:10,
			textAlign:'right',
			autocorrect:false,
			scrollable:false,
			clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS,
			borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE
		});

               var place_tf = Titanium.UI.createTextField({
			color:'#336699',
			hintText: 'Enter place',
			height:45,
			value: "",
			left:100,right:10,
			textAlign:'right',
			autocorrect:false,
			scrollable:false,
			clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS,
			borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE
		});

              function addFirstRow()
		{
			var row = Ti.UI.createTableViewRow({height:45});
			row.backgroundColor = '#ffffff';
			row.selectedBackgroundColor = '#ffffff';

                        /* creating a label to place in the tableView */
			var label = Titanium.UI.createLabel({
					color:'#000', height:45,left:10,text:'App Name', width:280,
					font:{fontSize:18,fontFamily:'Arial', fontWeight:'bold'}
			});

			row.add(label);
			row.add(name_tf );
			row.selectionStyle = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE;
			row.className = 'control';
			data_array[0] = row;
		}

               function addSecondRow()
		{
			var row = Ti.UI.createTableViewRow({height:45});
			row.backgroundColor = '#ffffff';
			row.selectedBackgroundColor = '#ffffff';

			var label = Titanium.UI.createLabel({
					color:'#000', height:45,left:10,text:'App Name', width:280,
					font:{fontSize:18,fontFamily:'Arial', fontWeight:'bold'}
			});

			row.add(label);
			row.add(place_tf );
			row.selectionStyle = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE;
			row.className = 'control';
			data_array[1] = row;
		}

                addFirstRow();    /* calling the above functions */
                addSecondRow();

              /* Creating a tableView and placing the TextBox and Label inside it
                  here data is the property for which we have to provide the data-source */

               var tableview = Titanium.UI.createTableView({
			data:data_array,
			height:260,
			width:320,
			backgroundColor:'transparent',
			scrollable:'false',
			style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
			top:25
		});

Now you have created a tableView with 2 controls in each row.
Make sure to add this to your window to view it.
Please leave your comments if this post was useful.

How to load a PDF in Titanium?

Loading a PDF file in Titanium may be sometimes our requirement for an application. For loading a PDF we need to have a webview inside a scrollview so that it is scrollable also. Take a look at this simple example.

/** creating a window **/
var window = Titanium.UI.createWindow{};
/** creating a webview **/
var my_webview = Titanium.UI.createWebView({     url :'my_pdf.pdf'      //specify the PDF path.....});
/** creating a scrollview **/var my_scrollView = Titanium.UI.createScrollableView({     views:[my_webview]   // placing the webview inside the scrollview...});
window.add(scrollView);

Please leave your valuable comments if this post was useful…..

HTTPClient in Titanium iPhone or ANDROID. How to check whether you request is complete?

function sendRequest()
{
	   var xhr = Titanium.Network.createHTTPClient();
	   xhr.onload = function()
	   {
		    if(this.status == 200)
		    {
		    	// this becomes true if your request is completed.
		    }
	   };
};
xhr.open('POST','http://your_domain.com/test.php');
xhr.send({ 'data':data});
xhr.onerror = function()
{
    Titanium.API.info('error');
    Titanium.UI.createAlertDialog({title:'Error ', message:'Please check your internet connection'}).show();
};