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

This is an old revision of the document!


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,

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.1427891062.txt.gz · Last modified: 2015/04/01 20:24 (external edit)