Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange bug in EditText.setText() method for RTL languages in Lenovo devices

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?

like image 837
Bobs Avatar asked Apr 11 '18 08:04

Bobs


2 Answers

.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.

like image 94
Daniyar Kayirbolatov Avatar answered Oct 24 '22 07:10

Daniyar Kayirbolatov


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

like image 39
Android_K.Doe Avatar answered Oct 24 '22 07:10

Android_K.Doe