DroidScript : Hello world

This is the classic. Very valuable when you have some background in computing and know some languages. The hello world program shows the differences with the languages you already know.

Hello world: the source

//Called when application is started.
function OnStart()
{
	//Create a layout with objects vertically centered.
	lay = app.CreateLayout( "linear", "VCenter,FillXY" );	

	//Create image 1/5 of screen width and correct aspect ratio.
	img = app.CreateImage( "Img/Hello World.png", 0.2, -1 );
	lay.AddChild( img );
	
	//Create a button 1/3 of screen width and 1/10 screen height.
	btn = app.CreateButton( "Press Me", 0.3, 0.1 );
	btn.SetMargins( 0, 0.05, 0, 0 );
	lay.AddChild( btn );
	
	//Set function to call when button pressed.
	btn.SetOnTouch( btn_OnTouch );
	
	//Add layout to app.	
	app.AddLayout( lay );
}

//Called when user touches our button.
function btn_OnTouch()
{
	//Show a popup message.
	app.ShowPopup( "Hello World!" );
	
	//Vibrate phone with a pattern (in milliseconds).
	//pause,vibrate,pause,vibrate...
	app.Vibrate( "0,100,30,100,50,300" );
}
   

And this is what 'happens' when you press the button

As you can see it doesn't take much own programming to make a hello world script. All functions prepended by the 'app' prefix belong to the DroidScript runtime. In this case it is vibrate. No need to make your own vibrator routine. Just use it. Droidscript is littered with pre-built functions.


Page created April 19, 2015 and