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

Simple Custom Search Bar

A simple custom search bar could be made up from a TextEdit control and a search button control, contained within a horizontal Linear Layout. The code below is a very simple quick implementation.

search.js
var txtSize = 22;
 
//Called when application is started.
function OnStart()
{
    lay = app.CreateLayout( "linear", "FillXY" ); 
    lay.SetBackground( "/Sys/Img/BlueBack.png" );
 
    //Create a layout to hold the search controls
    laySearch = app.CreateLayout( "Linear", "Horizontal, FillX, VCenter" ); 
    laySearch.SetBackColor( "#000000" );
    laySearch.SetPadding( 0, 0.01, 0, 0.01 );
 
    //Create a TextEdit control for entering search text
    txeSearch = app.CreateTextEdit( "", 0.85 );
    txeSearch.SetTextSize( txtSize );
 
    //Create a search button
    btnSearch = app.CreateText( "[fa-search]", 0.1, -1, "FontAwesome" );
    btnSearch.SetTextSize( txtSize );
    btnSearch.SetOnTouch( btnSearch_OnTouch );
 
    laySearch.AddChild( txeSearch );
    laySearch.AddChild( btnSearch );
    lay.AddChild( laySearch );
 
    //Add layout to app. 
    app.AddLayout( lay );
}
 
//Called when the search button is pressed
function btnSearch_OnTouch( ev )
{
    if( ev.action=="Down" )
    {
        this.SetTextSize( txtSize-4 );
    }
    else if( ev.action=="Up" ) 
    {
        this.SetTextSize( txtSize );
    
        var searchText = txeSearch.GetText();
        app.ShowPopup( "Search: " + searchText );
    }
}

(Sample posted by Chris Hopkin in the Droidscript Forum)


TextEdit Search

This function is designed to search for a string in a textEdit.

It starts searching from the current position of the cursor and, if it finds an occurrence of the string, positions the cursor immediately before the occurrence.

An area immediately following the cursor is highlighted as the selected area.

If it does not find an occurrence, the cursor is positioned at the beginning of the text, allowing the user to search again from there if required.

findtext.js
function findtext(thisedt,sought)
//thisedt = textEdit to be searched.
//sought = string to be searched for.
{
   //both toLowerCase for case-insensitive search
   var lc = thisedt.GetText().toLowerCase();
   sought = sought.toLowerCase();
   //current cursor position is start for search
   var curs = thisedt.GetCursorPos();
   //only compare against the searchable range
   lc = lc.slice(curs+1)
   // try to find next occurence of sought
   var pos = lc.search(sought);
   if ( pos > -1)
   { 
      //we found it
      pos  = pos + curs +1
      thisedt.SetCursorPos(pos)
      thisedt.SetSelection(pos, pos+sought.length);
   }
   else
   {
      //not found - go to start of text
      //user can search again from there if required.
      thisedt.SetCursorPos(0)
   }
}

This function can be used in an app in the manner shown in this sample code

samplefind.js
var edt,fnd;
//Called when application is started.
function OnStart()
{
    //Create a layout with objects vertically centered.
    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );    
    var hlay = app.CreateLayout( "linear", "Horizontal,FillX" );
    var btn = app.CreateButton("Find");
    btn.SetOnTouch(doFind);
    hlay.AddChild(btn);
    fnd = app.CreateTextEdit("show");
    fnd.SetHint("Find");
    hlay.AddChild(fnd);
    lay.AddChild(hlay);
    //Create a text label and add it to layout.
 
    var scrollEdit = app.CreateScroller( 1.0, 0.83, "" );
    //Create a textedit and add it to layout.
    edt = app.CreateTextEdit( "",1,-1,"Left,NoSpell" );
    edt.SetText(app.ReadFile("/android_asset/app.js"))
    //edt.SetBackColor("#ffcccccc");
    //edt.SetTextColor("#ff333333");
    scrollEdit.AddChild(edt);
    lay.AddChild( scrollEdit);
 
    
    //Add layout to app.    
    app.AddLayout( lay );
}
 
function doFind()
{
   app.HideKeyboard();
   var findstring = fnd.GetText();
   findtext(edt,findstring)
}
 
function findtext(thisedt,sought)
//thisedt = textEdit to be searched.
//sought = string to be searched for.
{
   //both toLowerCase for case-insensitive search
   var lc = thisedt.GetText().toLowerCase();
   sought = sought.toLowerCase();
   //current cursor position is start for search
   var curs = thisedt.GetCursorPos();
   //only compare against the searchable range
   lc = lc.slice(curs+1)
   // try to find next occurence of sought
   var pos = lc.search(sought);
   if ( pos > -1)
   { 
      //we found it
      pos  = pos + curs +1
      thisedt.SetCursorPos(pos)
      thisedt.SetSelection(pos, pos+sought.length);
   }
   else
   {
      //not found - go to start of text
      //user can search again from there if required.
      thisedt.SetCursorPos(0)
   }
}
sample_code/textedit_search.txt · Last modified: 2015/08/05 14:40 (external edit)