Strange error in EditText.setText () method for RTL languages ​​on Lenovo devices

I received an error message from my customers about my Android application on Lenovo devices. I noticed that when I set the application language to RTL, such as Persian or change the Android OS language to Persian, the method setTex() EditTexteither Textviewinserts two additional characters at the beginning and at the end of the original line. For instance:

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 issue only occurs on Lenovo devices. The method setText()adds LEFT-TO-RIGHT OVERRIDE (U+202D)at the beginning and POP DIRECTIONAL FORMATTING (U+202C)at the end of my line in RTL mode. This causes big problems in my application. I have many methods setText(). Is there a short solution to solve this problem?

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

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

+4
source share
2 answers

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

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

Firstly, it is not String, but only CharSequence. Thus, it is based on formatting , which can affect the size of the calculations.

+1
source

The best solution for this is indicated in the comments that should use .trim(). But according to what was said,

I need a shorter solution. I need to add many trim () methods or something like that

, .trim() . , IDE. , .

CTRL + SHIFT + R

myEditText.getText().toString()

, - myEditText.getText(). toString(). lenght()

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

- ;

myEditText.getText().toString();

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

, editText. ,

+1

Source: https://habr.com/ru/post/1696050/


All Articles