Skip to content

Cursor position resets on toggling value of secure property of TextField on Android #3181

@shripalsoni04

Description

@shripalsoni04

Did you verify this is a real problem by searching [Stack Overflow]

Yes.

Tell us about the problem

In my nativescript-angular project, I have a TextField control as follows:

<TextField
    hint="Password"
    [secure]="!isPasswordVisible">
</TextField>

Now, when we change isPasswordVisible value programmatically, it works great on iOS. But on android it always resets the cursor position to the first character.

I tried to find the root cause of the issue and found that whenever setInputType is called, android will reset the cursor position to the first character.

I checked the code of text-field.android.js and the issue is we are calling setInputType but we are not setting the cursor to its original position. This results in cursor resets to first character. So just getting the cursor position before calling setInputType and setting that cursor position after calling setInputType will solve this issue as shown below.

text-field.android.js

function onSecurePropertyChanged(data) {
   var textField = data.object;
   ...
   var cursor = textField.android.getSelectionStart();
   ...
   textField.android.setInputType(newInputType);
   textField.android.setSelection(cursor);
}

Edit

I also noted that currently when secure is set to false, the input type is being set to TYPE_TEXT_VARIATION_NORMAL. This opens a different soft keyboard than that is visible when secure is true. So it will be great if we can have some provision so that when password is visible (secure is false), the inputType is set to TYPE_TEXT_VARIATION_VISIBLE_PASSWORD instead of TYPE_TEXT_VARIATION_NORMAL. This will keep the soft keyboard same irrespective of whether password is visible or not.

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.4.0
  • Cross-platform modules: 2.4.0
  • Runtime(s): 2.4.1

Let me know if you want me to create a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions