I want to display connected AND colored arabic letters on a android view (webview or textview). First, I've used TextView without color and the arabic displayed correctly.
When I use spannableString, the colored letter disconnect from the word.
Secondly, I've used a WebView with html and font tags. I've got the same result.
Each time I try to put color on a letter in a word, this letter display disconnected from the word.
Does anyone has a solution to this problem ?
Android version>4.0
Thanks God after lots of struggle i found the solution. Use open source library:
1: it will return the android text in the form of unicode.
2:then u apply the span colors like this :
final SpannableStringBuilder ssb = new SpannableStringBuilder();
final int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
//converting arabic text to unicode chars
String dd=ArabicUtilities.reshape("بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ");
ssb.append(dd);
//applying colors
ssb.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, flag);
ssb.setSpan(new ForegroundColorSpan(Color.RED), 1, 2, flag);
ssb.setSpan(new ForegroundColorSpan(Color.GREEN), 2, 3, flag);
ssb.setSpan(new ForegroundColorSpan(Color.BLUE), 3, 4, flag);
//seting formated spanned text
textview.setText(ssb);
Usama and danial both have the right idea, but they have not explained it very clearly. If you use unicode arabic letters (0x06--), recent versions of android will automatically join the letters together where required, but this is done after applying the coloring, and the colouring breaks the connection between letters.
You have to convert the unicode arabic letters (0x06--) into Arabic presentation B (0xFE--), and apply the connections yourself as you convert, then use set the colours on the already-connected letters. The library that Usams suggests looks like a good way of converting from unicode arabic to presentation B characters.
You must change the letters to Unicode.
For example, use \ufe91 for ب and use \ufeb4 for س, and then change the color.
If your text is big, it may be slow to show in Android.
I think it has problems when showing big Spannable text.
来源:https://stackoverflow.com/questions/17865392/android-connected-and-colored-arabic-letters