Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-19639] Android: TextField setSelection(0,end) has to be called twice to select text

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-04-22T16:31:01.000+0000
Affected Version/sn/a
Fix Version/sRelease 5.4.0
ComponentsAndroid
Labelsandroid, selection, textfield
ReporterMichael Gangolf
AssigneeHans Knöchel
Created2015-09-24T08:16:55.000+0000
Updated2016-06-28T23:14:18.000+0000

Description

When trying to select the whole text in a TextField I use the setSelection function:
function selectText(e){
   $.tf_1.setSelection(0,$.tf_1.value.length)
}

$.btn.addEventListener("click",selectText)
$.index.open();
<Alloy>
	<Window class="container">
		<TextField id="tf_1" value="123456"></TextField>
		<Button id="btn">Select text</Button>
	</Window>
</Alloy>
{noformat} "TextField": { right:50, left:50, color:"black" } ".container": { layout:"vertical", backgroundColor:"white" } {noformat} It's only working when the cursor is at the beginning. When it's in the middle or end it will just move the cursor to front and I have to click the button again to select the text. From the function call (0, length) I expect to start from 0 and select the whole text in one go Android 5 (Nexus 4) SDK 5.0.0.GA _Workaround:_ use
function selectText(e){
  $.tf_1.setSelection(0,$.tf_1.value.length)
  _.delay(function(){
    $.tf_1.setSelection(0,$.tf_1.value.length)
  },200);
}

Comments

  1. Michael Gangolf 2016-04-02

    PR: https://github.com/appcelerator/titanium_mobile/pull/7905 Bug caused by IME (input method editor), see: http://stackoverflow.com/a/35527348/5193915
  2. Hans Knöchel 2016-04-02

    [~michael] The PR looks great! Can you provide a demo code that can be ran without Alloy? I can create one if it exceeds your available time, just let me know.
  3. Michael Gangolf 2016-04-02

    no problem:
       var win = $.UI.create("Window",{
           layout:"vertical",
           backgroundColor:"#fff"
       });
       
       var tf = $.UI.create("TextField",{
           value:"12345678"
       });
       
       function selectText(e){
          tf.setSelection(0,tf.value.length)
       }
       
       function selectText2(e){
          tf.setSelection(2,5)
       }
       
       var btn = $.UI.create("Button",{
           title:"Select all"
       });
       
       var btn2 = $.UI.create("Button",{
           title:"Select 2-5"
       });
       
       win.add(tf);
       win.add(btn);
       win.add(btn2);
       
       btn.addEventListener("click",selectText)
       btn2.addEventListener("click",selectText2)
       
       
       win.open();
       
    *Steps to reproduce* * Click inside the textview so the cursor is not in front * Click a button to set the selection *without the patch* cursor moves to the beginnen, no selection. You have to press the button again (it will change the cursor from "selection cursor" to the normal one) *with the patch* text gets selected
  4. Hans Knöchel 2016-04-22

    Approved! Thank you [~michael], great work!
  5. Lokesh Choudhary 2016-06-28

    Verified the fix. Closing. Environment: Appc Studio : 4.7.0.201606150733 Ti SDK : 5.4.0.v20160617074028 Ti CLI : 5.0.9 Alloy : 1.9.0 MAC El Capitan : 10.11.4 Appc NPM : 4.2.7-2 Appc CLI : 5.4.0-20 Node: 4.4.4 Nexus 6 - Android 6.0.1

JSON Source