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 EditText
s or Textview
s 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?
.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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With