Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25061] Android: setSelection method on textField fires change event, and value is not changing

GitHub Issuen/a
TypeBug
PriorityCritical
StatusResolved
ResolutionFixed
Resolution Date2017-08-08T21:34:17.000+0000
Affected Version/sRelease 6.1.1, Release 6.1.2
Fix Version/sn/a
ComponentsAndroid
Labelsandroid
ReporterPeter Lancaster
AssigneeJoshua Quick
Created2017-08-02T15:11:24.000+0000
Updated2017-08-08T21:34:17.000+0000

Description

Issue description

Using the setSelection method on a textField causes a _*change*_ event to be fired (when the value has not been updated).

Steps to reproduce

To reproduce this issue with a textField variable, the following code snippet should work and show the issue in action, once the user focuses on a given field:
// within method setting up textField event listeners

textFieldVar.addEventListener("focus", $.handleTextFieldEvent);

// elsewhere

$.handleTextFieldEvent = function(e) {
    Ti.API.info(e.type + " event fired");
    if(e.type === "focus"){
      Ti.API.info("set selection of text");
      e.source.setSelection(0, 1);
    }
};
This should lead to output like:
[INFO]  focus event fired
[INFO]  set selection of text
[INFO]  change event fired
Within a code flow that uses an auto-advancing form (based on single values within textField elements with change events that trigger a move onto the _next_ textField element), this effectively causes the form to be cycled through, and prevents user input.

Comments

  1. Peter Lancaster 2017-08-04

    Hi guys - is there any update on this? The only workaround we currently have for this would sacrifice usability and functionality, which is highly undesirable.
  2. Eric Merriman 2017-08-04

    Hello [~plancaster] We have looked at this today and were able to reproduce. The change that triggers this behavior was a workaround for a google bug and was implemented 1 year ago. See https://stackoverflow.com/questions/22933803/edittexts-selectall-does-not-select-the-text-instead-cursor-is-moved-to-posi/35527348#35527348 for the description of the issue and https://github.com/appcelerator/titanium_mobile/blame/master/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java#L683 for the section of the SDK where it was implemented. Basically it does a replace, which would fire the change event. At this point I don't have a good workaround for you, but as soon as I have a fix we can get you a build to try. 6.2.0 is our next build, scheduled for the end of this month. I will work with support to see what other options are available and will get you that info through them. I will update this ticket should we find a workaround in the meantime. Expect an update early next week.
  3. Lokesh Choudhary 2017-08-04

    Verified the issue is reproducible.
       var win = Ti.UI.createWindow({
           backgroundColor: 'grey',
           layout: 'vertical'
       });
       
       var txtfld = Ti.UI.createTextField({
       	top: 30,
       	width: 50,
       	value: 'Hello'
       });
       
       txtfld.addEventListener('focus', function(e) {
       	Ti.API.info(e.type + " event fired txtfld");
       	if(e.type === "focus"){
       	  Ti.API.info("set selection of text txtfld");
       	  e.source.setSelection(0, 1);
       	}
       });
       
       txtfld.addEventListener('change', function(){
       	Ti.API.info('=========================================');
       	Ti.API.info('Change Fired on txtfld');
       });
       
       win.add(txtfld);
       win.open();
       
    Result:
       [INFO] :   focus event fired txtfld
       [INFO] :   set selection of text txtfld
       [INFO] :   =========================================
       [INFO] :   Change Fired on txtfld
       
    Studio Ver: 4.9.1.201707200100 SDK Ver: 6.1.2.GA OS Ver: 10.12.3 Xcode Ver: Xcode 8.3.3 Appc NPM: 4.2.9 Appc CLI: 6.2.3 Ti CLI Ver: 5.0.14 Alloy Ver: 1.9.13 Node Ver: 6.10.1 Java Ver: 1.8.0_101 Devices: ⇨ google Nexus 5 --- Android 6.0.1
  4. Joshua Quick 2017-08-08

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/9281 PR (6.2.X): https://github.com/appcelerator/titanium_mobile/pull/9282
  5. Lokesh Choudhary 2017-08-08

    FR Passed for both branches. PR merged.

JSON Source