Back

Toggle Buttons

Create toggle buttons using the CreateToggle method of the app object:

 btn = app.CreateToggle( text, width, height, options );

You can allow the toggle button to auto-size by leaving out the dimensions or you can specify a width and height as decimal fractions of the screen's width and height.

Use the SetOnTouch method of your button object to set the name of a function you want to be called when the button is touched.

You can get the state of the toggle button at any time using the GetChecked method of the button. The button state is also passed into your OnTouch callback function as a parameter every time the button is touched.

Example

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  btn = app.CreateToggle( "Toggle Me" );
  btn.SetOnTouch( ShowState );
  lay.AddChild( btn );

  app.AddLayout( lay );
}

function ShowState( isChecked )
{
  app.ShowPopup( "Checked = " + isChecked, "Short" );
}
  Copy   Copy All    Run   

The following methods are avaiable on the Toggle object:

 SetVisibility( visibility )
 GetVisibility()
 SetPadding( left, top, right, bottom )
 SetMargins( left, top, right, bottom )
 SetPosition( left, top, width, height )
 SetSize( width, height )
 GetWidth()
 GetHeight()
 SetOnTouch( callback )
 SetText( text )
 GetText()
 SetTextColor( colorCode )
 SetTextSize( size )
 SetChecked( checked )
 GetChecked()