Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16198] Android: setSelection() params reversed

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2015-04-13T16:19:34.000+0000
Affected Version/sRelease 3.2.0
Fix Version/sRelease 3.3.0
ComponentsAndroid
Labelstriage
ReporterTim Poulsen
AssigneeBiju pm
Created2014-01-16T02:12:13.000+0000
Updated2015-04-13T16:19:38.000+0000

Description

Expected behavior

Per the docs, setSelection() takes two parameters, start and end, in that order. Thus, to select all the text in a text field, I would expect to use setSelection(0, $.txt.value.length) http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.TextField-method-setSelection

Actual behavior

On Android, it appears that those parameters are reversed. The following code will select all the text in the text field on iOS & Android
$.txt.addEventListener('focus', function(e) {
   if(OS_IOS) {
      $.txt.setSelection(0,$.txt.value.length);
   } else {
      // params are backward on Android
      $.txt.setSelection($.txt.value.length, 0);
   }
});

Comments

  1. Biju pm 2014-01-16

    Both setSelection(0, $.txt.value.length) and setSelection($.txt.value.length, 0) are work for me . I used the bellow Test case. Tested with: Titanium Studio, build: 2.1.2.201208301612 Titanium SDK version: 3.2.0 Tested on Device: Samsung galaxy s duos Android version: 4.0.4 And Android 4.2.2 emulator , Android 2.3.3 emulator ,
       Titanium.UI.setBackgroundColor('#000');
        
       // create base UI tab and root window
       var window = Titanium.UI.createWindow({  
           title:'Select Text',
           backgroundColor:'#fff'
       });
        
       var textfield = Titanium.UI.createTextField({
           color:'#336699',
           height:35,
           width:200,
           top:10,
           left:10,
       });
        
       var button1 = Titanium.UI.createButton({
           title:'Select',
           top:50,
           left:10
       });
        
       button1.addEventListener('click', function() {
           textfield.setSelection(0, textfield.value.length);
       });
        
           
       window.add(textfield);
       window.add(button1);
       window.open();
       
  2. Tim Poulsen 2014-01-16

    Additional test: Xoom tablet running 4.1.2 The code has to be entered (with params reversed) as shown in my sample so that text is selected when you tap the field. Note that my test case used Alloy, not classic, and used the focus event not click.
  3. Lokesh Choudhary 2014-04-22

    I tried to reproduce the issue using an alloy app on android 4.1.2, 4.2.2 & 4.4.2 android devices & I could see the text is selected on focus in both cases when I used "$.txt.setSelection($.txt.value.length,0);" & $.txt.setSelection(0,$.txt.value.length);". I ran it on an iOS simulator too & it showed me the same results as android. Environment: Appc Studio : 3.2.3.201404162038 Ti SDK : 3.2.3.v20140417114627 Mac OSX : 10.8.5 Alloy : 1.3.1 CLI - 3.2.3-beta2
  4. Tim Poulsen 2014-04-22

    I can no longer reproduce the original issue. I tested on my Xoom tablet (specs above) and in the Genymotion emulator (Galaxy Nexus 4.2.2). You can close the ticket.
  5. Eric Merriman 2014-04-22

    Based on the comments and feedback from Ingo, marking as invalid and closing.

JSON Source