10

I received a bug report from my customers about my android application in Lenovo devices.

I noticed when I set the locale of app to a RTL language like Persian or change the language of Android OS to Persian, the setTex() method of EditTexts or Textviews inserts two extra characters at the beginning and the end of the original string.

For example:

String myString1 = "original string";
int length1 = myString1.length(); // length1 is 15

myEditText.setText(myString1);
String myString2 = myEditText.getText().toString();
int length2 = myString2.length(); // length2 is 17

This problem occurs only on Lenovo devices. The setText() method adds LEFT-TO-RIGHT OVERRIDE (U+202D) at the beginning and POP DIRECTIONAL FORMATTING (U+202C)at the end of my string in RTL mode. This causes big problems in my application. I have lots of setText() methods. Is there any short solution for solving this this problem?

Device info: Lenovo Tablet TB-8504X, Android 7.1.1

Update: Is the problem with Android OS? Can I find any fix for the device?

SAYE
  • 1,247
  • 2
  • 20
  • 47
Bob
  • 22,810
  • 38
  • 143
  • 225
  • 1
    have you tried `trim()` ? – John Joe Apr 11 '18 at 08:40
  • @JohnJoe I need shorter solution. I have to add lots of trim() method or something like that. – Bob Apr 11 '18 at 08:42
  • Not an android dev, so not sure if this may help or not. Please see if https://stackoverflow.com/questions/18008189/how-to-get-text-direction-in-android-and-change-layout-dynamically-according-to is of any help? – Tarun Lalwani Apr 15 '18 at 13:35
  • Is your problem with setText() (i.e. layout on the screen), or with getText(), i.e. read what the user changed in the text box? – Alex Cohn Apr 15 '18 at 19:22
  • @AlexCohn The problem is with `setText()`. Because after calling `setText()` you can erase extra characters by back space. – Bob Apr 16 '18 at 10:15
  • It could be pretty tricky to intercept and fix all calls to `setText()`, including the implicit ones. But if your problem can be resolved on the side of `getText()`, there may be a reasonable hack to trim its output. You can get an idea from [this blog](http://shadowwhowalks.blogspot.co.il/2013/02/android-replacing-system-classes.html). – Alex Cohn Apr 16 '18 at 21:47
  • @AlexCohn I want to know the source of this bug. Why this problem occurs only on this device? Can I find any fix for the OS? – Bob Apr 17 '18 at 05:26
  • Unfortunately I could not find a similar device around me to play with. The OS modification are probably not public: the Android license does not require this. You can try to file a complaint on Lenovo's [Android Tablets](https://forums.lenovo.com/t5/Lenovo-Android-based-Tablets-and/bd-p/lt02_en) board. – Alex Cohn Apr 17 '18 at 08:32
  • create your custom editText and use trim there. – Atif AbbAsi Apr 19 '18 at 05:49

2 Answers2

3

.length() and .getText().length() they are identical

However .getText().toString().length() is different in some cases.

First, it is, not String, nothing but CharSequence. So, it is based on formatting, that may cause to affect on size of calculation.

  • This. I am pretty sure, that if you'd used ``CharSequence`` instead of ``String``, the length in both cases would be identical. – Grisgram Apr 21 '18 at 15:54
2

The best solution for this is stated in the comments which is to use .trim(). But according to what you said,

I need shorter solution. I have to add lots of trim() method or something like that

it seems like your problem the time consumption of locating each uses and adding .trim() to it. If that is the case, just use the replace function of your IDE. You can avoid unnecessary changes by specifying the whole line.

Press CTRL + SHIFT + R

myEditText.getText().toString()

with space in the end to avoid something like myEditText.getText().toString().lenght()

replace by

myEditText.getText().toString().trim()

and the other is the line with ;

myEditText.getText().toString();

replace to

myEditText.getText().toString().trim();

So this covers all the uses of this editText's value usage. Hope this helps

Android_K.Doe
  • 753
  • 1
  • 4
  • 11