Android : Connected and colored arabic letters

笑着哭i 提交于 2019-12-06 00:38:04

Thanks God after lots of struggle i found the solution. Use open source library:

Better-Arabic-Reshaper

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.

danial eghbali

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!