User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


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:folder_picker

This is an old revision of the document!


Sample folder picker

This is a simple dialog to allow a user to choose a file folder.

See also Sample File Picker

The code

folderpicker.js
//Called when application is started
function  OnStart()
  {     //Create a layout with objects vertically centered.
        
    lay  =  app.CreateLayout( "linear",  "Vertical,FillXY" );    
 
         //Create a text label and add it to layout.
        
    btn  =  app.CreateButton( "FolderPicker demo" );    
    lay.AddChild( btn );    
    btn.SetOnTouch(btn_OnTouch);         //Add layout to app.    
        
    app.AddLayout( lay );    
    pick  =  new  FolderPicker(mycallback);     // don't show hidden files
        
    pick.SetHideFiles(true);
  } //function Onstart()
 
function  btn_OnTouch()
  {    
    pick.SetFolder("/sdcard");    
    pick.Show();    
  } //function btn_OnTouch()
 
function  mycallback(fullpath)
  {  
    app.Alert("user chose "  +  fullpath)
  } //function mycallback()
 
function  FolderPicker(Callback, basePath)
  {  
    var  self  =  this;  
    this.basePath  =  basePath  ||  "/sdcard";  
    this.callback  =  Callback  ||   function() {};  
    this.FolderPath  =  this.basePath;  
    this.hideHiddenFiles  =  false;  
    this.dlg  =  app.CreateDialog(this.basePath);  
    this.lay  =  app.CreateLayout( "linear",  "horizontal,fillxy" );    
    this.lstFolds  =  app.CreateList("blank" ,  0.7,  0.7 );  
    this.lstFolds.parent  =  self;  
    this.lstFolds.SetOnTouch(FolderPicker_NewFolder);    
    this.lay.AddChild(this.lstFolds);
 
      
    var  vlay  =  app.CreateLayout( "linear",  "vertical,fillxy" );  
    this.okBtn  =  app.CreateButton("Ok");  
    this.okBtn.parent  =  self;  
    this.okBtn.SetOnTouch(FolderPicker_Select);  
    vlay.AddChild(this.okBtn);    
    this.cancelBtn  =  app.CreateButton("Cancel");  
    this.cancelBtn.parent  =  self;  
    this.cancelBtn.SetOnTouch(FolderPicker_Select);  
    vlay.AddChild(this.cancelBtn);  
 
      
    this.lay.AddChild(vlay)
 
       this.dlg.AddLayout(this.lay);
 
      
    this.Show  =   function()
    {    
      self.dlg.Show();  
    }  
    this.SetHideFiles = function(val)
    {    
      if (val  ==  undefined)  val  =  true;    
      self.hideHiddenFiles  =  val;  
    }  
    this.Hide  =   function()
    {    
      self.dlg.Hide();  
    }  
    this.GetFolder  =   function()
    {    
      return  self.FolderPath;  
    }  
    this.SetFolder  =   function(folderPath)
    {    
      self.FolderPath  =  folderPath;    
      self.dlg.SetTitle(folderPath);    
      app.ShowProgress( "Loading..." );    
      var  lst  =  app.ListFolder(folderPath);    
      lst.sort(function(x, y)
      {
        return  (x.toLowerCase()  >  y.toLowerCase()) ? 1 : -1
      });     //var dirlist=[];
          
      self.lstFolds.SetList("");    
      if ( self.FolderPath  !=  self.basePath )       self.lstFolds.AddItem("..", null, "folder");    
      var  ths  =  lst.shift();    
      while  (undefined  !=  ths) 
      {      
        if  ((! self.hideHiddenFiles)  ||  (ths.indexOf(".")  !=  0))
        {        
          var  pth  =  folderPath  +  "/"  +  ths;         //if (app.IsFolder(pth))dirlist.push(ths);
                  
          if  (app.IsFolder(pth))            self.lstFolds.AddItem(ths, null, "folder")      
        }      
        ths  =  lst.shift();    
      }        // self.lstFolds.SetList(dirlist.join(","));
          
      app.HideProgress();  
    }  
  } //function FolderPicker()
 
function  FolderPicker_NewFolder(fil)
  {  
    var  par  =  this.parent;  
    var  pth  =  par.GetFolder();  
    if  (fil  !=  "..") 
    {      
      pth  +=  "/"  +  fil  
    }  
    else
    {     
      if ( pth  ==  par.basePath  ||  pth  ==  "/" )
      {       
        par.Hide()        return;     
      }     
      var  tst  =  pth.split("/");     
      tmp  =  tst.pop();     
      pth  =   (tst.join("/"));  
    }    
    par.SetFolder(pth);
  } //function FolderPicker_NewFolder()
 
function  FolderPicker_Select(fil)
  {  
    var  par  =  this.parent;  
    var  pth  =  par.GetFolder();  
    par.Hide();  
    if (this.GetText()  ==  "Ok")  par.callback(pth );
  } //function FolderPicker_Select()
sample_code/folder_picker.1417900408.txt.gz · Last modified: 2014/12/07 05:13 (external edit)