Numbers inside TextView
are getting reversed when formatted in RTL.
When numbers are at the end of a text inside a TextView
they getting reversed. How can I solve this programmatically?
As an example, the numbers below are reversed:
They should be displayed like:
The misunderstand: Digits in RTL languages like ARABIC should be written from RTL with the arabic digits to avoid any problems i.e: "تم إرسال رسالة نصية للرقم ١٢٣٤" Note that I wrote "رسالة نصية" NOT "SMS رسالة".
The problem and it's solution: Mixing more than one direction languages required more steps, you need to tell the system "hey this is RTL word, add as it to sequence". So you may need to do this implicitly, i.e:
\u200f + تم إرسال رسالة نصية إلى + number
Consider StringBuilder: It's very painful for developer to develop something for RTL language using plus(+) notation, this much confusing and error prone.
A better way:
builder.append("\u061C").append(" تم إرسال رسالة نصية لـ").append("\u200E").append("+0123456789")
Consider BidiFormatter: Utility class for formatting text for display in a potentially opposite-directionality context without garbling
Example:
String text = "{0} تم إرسال رسالة نصية لـ ";
String phone = BidiFormatter.getInstance().unicodeWrap("+961 01 234 567");
String result = MessageFormat.format(text,phone);
Now, result
will be formatted properly.
More examples on how BidiFormatter work.
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