DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


Sidebar

News

Version1.42 is out since December 2nd 2016


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:img_toggle_button

Table of Contents

Image button toggle

This code was written in response to a request for a Toggle Image Button.

The main body of the code is the function

  CreateImageToggle(img1,img2)

The images can be passed either as file-paths or as images The sample code passes one of each.

The first image decides the size of the button.

In case it is not obvious, the actual toggling is done in the OnTouchDown handler.

You could move this to any of the OnTouch events but be aware that some of them (such as SetOnTouch) may fire more often than you expect.

The code

toggle.js
//Called when application is started.
function  OnStart()
{     // Create layout
      
  lay  =  app.CreateLayout( "linear",  "VCenter,FillXY" );             // Create resized first image for the button
      
  var  pauseImg  =  app.CreateImage( "/Sys/Img/Droid1.png", 0.1, -1);        //Create the button with OnTouchDown event
      
  btn  =  CreateImageToggle(pauseImg,  "/Sys/Img/Droid2.png" ) ;    
  btn.SetOnTouchDown(btnToggle);        
  lay.AddChild( btn );         //Add layout to app.    
      
  app.AddLayout( lay );
}
 
function  btnToggle(ev)
{   
  this.Toggle  =  ! this.Toggle;   
  if (this.Toggle)
  {      
    this.SetImage(this.imgTrue);      
    app.ShowPopup("Playing");   
  } 
  else
  {      
    this.SetImage(this.imgFalse);      
    app.ShowPopup("Paused");   
  }
}
 
function  CreateImageToggle(img1, img2)
{   // accepts images or file-paths
     //img1 represents false, img2 true(toggled)
    
  if  (typeof  img1  ==  "string") 
  {     
    img1  =  app.CreateImage(img1);  
  }  
  if  (typeof  img2  ==  "undefined")
  {    
    img2  =  img1;  
  } 
  else  if  (typeof  img2  ==  "string") 
  {    
    img2  =  app.CreateImage(img2, img1.GetWidth(), img1.GetHeight());  
  }  
  var  obj  =  app.CreateImage(null, img1.GetWidth(), img1.GetHeight());  
  obj.SetImage(img1);
  obj.Toggle  =  false;  
  obj.imgFalse  =  img1;
  obj.imgTrue  =  img2;  
  return  obj;
}

Note

This code has had only rudimentary testing. You may well be able to improve it

sample_code/img_toggle_button.txt · Last modified: 2015/01/13 09:55 by stevegarman