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

This is an old revision of the document!


New Folder Dialog

Sometimes you have to create new folder in a path.
Its easy with if you use a dialog.

(look for the example to create a new file too)

Save the DlgNewFolder.js in the Droidscriptfolder where you need the file.
You can call the dialog after you load the Script into your app at the OnStart() Event.

DlgNewFolder.js
/********************************************************************
 * CreateNewFolder Dialog
=====================================================================
 * Creation date: 23-01-2015 by octazid
 * Last update: 23-01-2015 by octazid
=====================================================================
 * Simple dialog to create a new folder
********************************************************************/
 
 
//*** CreateNewFolder Dialog***//
function NewFolderDialog(savepath)
{
  var self = this;
  this.Fdlg = app.CreateDialog("Create a new Folder:");
  this.Flay = app.CreateLayout( "Linear", "FillXY" );
  this.Ftxtpath = app.CreateText(savepath,0.9,0.02,"Left,Autoscale");
  this.Ftxtpath.SetMargins(0,0.01,0,0);
  this.Ftxtpath.parent = self;
  this.Flay.AddChild(this.Ftxtpath);
  this.Fedt = app.CreateTextEdit("",0.9,0.05,"Left");
  this.Fedt.SetTextSize(18);
  this.Fedt.SetHint("Foldername");
  this.Fedt.parent = self;
  this.Flay.AddChild(this.Fedt);
  this.Fbtnlay = app.CreateLayout( "Linear", "Horizontal,FillXY" );
  this.Fdlgbtn = app.CreateButton("OK", 0.45);
  this.Fdlgbtn.parent = self;
  this.Fdlgbtn.SetOnTouch(Fdlgbtn_OnTouch);
  this.Fbtnlay.AddChild(this.Fdlgbtn);
  this.FdlgbtnCancel = app.CreateButton("Chancel",0.45);
  this.FdlgbtnCancel.parent = self;
  this.FdlgbtnCancel.SetOnTouch(Fdlgbtn_OnTouch);
  this.Fbtnlay.AddChild(this.FdlgbtnCancel);
  this.Flay.AddChild(this.Fbtnlay)
  this.Fdlg.AddLayout(this.Flay);
  this.Show = function(){self.Fdlg.Show();}
  this.Hide = function(){self.Fdlg.Hide();}
}//function NewFolderDialog()
 
 
// Called if a button is touched
function Fdlgbtn_OnTouch()
{
    var par = this.parent;
    folder = par.Fedt.GetText();
    path = par.Ftxtpath.GetText();
    if(this.GetText()  == "OK") CreateFolder(folder, path);
    par.Hide();
}//function Fdlgbtn_OnTouch
 
 
// Called if Button Ok is touched
function CreateFolder(foldername, path)
{
    if (foldername != "")
    {
    //Replace illegal letters
    foldername = foldername.replace(/(=|\\|\/|\*|:|,|;|\+|<|>|\"|\[|\]|\?|\|)/g,"");  
 
    //Replace double Whitespaces   
    foldername = foldername.replace(/(\s+)/g," "); 
 
    //Replace Whitespaces at the start and at the end            
    foldername = foldername.trim();
 
        //Check if Folder exists. If yes, show a message
        if (app.FolderExists(path + foldername))
        {
        app.Alert("Please select another Foldername!", 
                  "Error - Folder already exists!");
        }
        else
        {
        //Create the folder
        app.MakeFolder(path + foldername);
        //show a message if creation was ok
        if (app.FolderExists(path + foldername))
            app.ShowPopup("Folder Created!");
        }
    }
}//function CreateFolder

In your app you have to do just three things,

1. Load the Script with

  app.LoadScript("DlgNewFolder.js") 

2. Create a Button to call the Dialog and add it to the layout

  btn = app.CreateButton( "Create a new Folder", 0.3, 0.1, "Alum" );
  btn.SetOnTouch(CreateNewFolder);
  lay.AddChild( btn );

3. Write a function to call the Dialog

  //Create a new Dialog 
  function CreateNewFolder()
  {
      // Change the path if you need
      DlgNewFolder = new NewFolderDialog("/mnt/sdcard/");
      DlgNewFolder.Show();    
  }

Or you can use this Example.

exampleapp.js
//Called when application is started.
function OnStart()
{
    app.LoadScript("DlgNewFolder.js")      
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );    
    btn = app.CreateButton( "Create a new Folder", 0.3, 0.1, "Alum" );
    btn.SetOnTouch(CreateNewFolder);
    lay.AddChild( btn );
    app.AddLayout( lay );    
} 
 
 
//Create a new Dialog 
function CreateNewFolder()
{
    // Change the path if you need
    DlgNewFolder = new NewFolderDialog("/mnt/sdcard/");
    DlgNewFolder.Show();    
}
sample_code/new_folder.1423135739.txt.gz · Last modified: 2015/02/05 19:29 (external edit)