Search code examples
titaniumappceleratortitanium-mobileappcelerator-mobile

Unable to move cursor to position 0 in Titanium.UI.TextArea


  • I have a TextArea in a modal Window. When the modal window is displayed, I want the focus to be set to this TextArea. I also want the TextArea to have default text when it is displayed & the cursor to be at the beginning of this text.
  • I call focus() on TextArea when modal window is displayed & in the focus() handler, I set the text that I want & call setSelection(0, 0) to move the cursor to position 0.
  • This doesn't seem to be working as the cursor remains at the end of the set text.
  • I am using the latest version of the SDK.
  • It would be great if someone could help me fix this issue. Thanks!

Titanium SDK: 3.0.2 Target platform: IOS only

Here's the code:

// 'statusUpdateArea' is my TextArea

$.tabbedBarNav.addEventListener('click',function(e) 
{
   statusUpdateArea.focus();
}

statusUpdateArea.addEventListener('focus',function() 
{
   statusUpdateArea.setValue(" -  I am here'");

   //API to set cursor at beginning doesn't work!!!!!! [or I don'tknow how to use it :(   ]
   statusUpdateArea.setSelection(0, 0);

});

Solution

  • Don't focus. setSelection will focus the text area for you. Focusing is preventing the selection from being properly set. Uncomment the .focus call to see it not work.

    Try the following. It works for me on iOS with Titanium SDK 3.x.

    var win = Ti.UI.createWindow({
        backgroundColor: '#fff'
    });
    var textArea = Ti.UI.createTextArea({
        value: 'Some text.'
    });
    win.add(textArea);
    win.addEventListener('open', function(evt) {
        // textArea.focus();
        textArea.setSelection(0, 0);
    });
    win.open();