73

I am getting Arabic text from server successfully. Retrieved text I want display in code but its showing boxes instead of Arabic text. Assume that t array values are Arabic text from server.

string[] t={" "};
Textview tv = (Textview) findviewByid(R.id.text);
tv.setText(t[0]);
Bob
  • 22,810
  • 38
  • 143
  • 225
babi
  • 779
  • 1
  • 6
  • 6
  • 1
    There is same question asked in SO manytimes. just googled. – user370305 Nov 01 '11 at 05:22
  • 1
    when i searched they guide like to use typeface,if i am using arabic text in my app i can use that ttf font.but in my case what is the way to avoid getting box box like text – babi Nov 01 '11 at 05:24
  • possible duplicate of [Arabic languge in android](http://stackoverflow.com/questions/4498302/arabic-languge-in-android) – Jacob Nov 15 '11 at 04:39

4 Answers4

136

Android 2.1 does not have Arabic font.

Android 2.2 has Arabic font but does not show your word correctly.

Android 3.x supports Arabic completely.

For Android 2.1 you must set the typeface Farsi.GetFarsiFont(this) and then use Farsi.Convert("سلام")

For Android 2.2 you do not need setting font but must use Farsi.Convert("سلام")

And for Android 3.x forget all of the above solutions ;). But you can change the font if you do not like the 3.x Arabic font.

so do the following steps:

1) At first add "TAHOMA.TTF" in assets folder

2) use the following class to get the font and converting your Arabic or Farsi text to a good form for showing in your activities.

import android.content.Context;
import android.graphics.Typeface;

public final class Farsi {

    public static boolean isFarsiConversionNeeded = true;

    private final static String szLamAndAlef = Character
            .toString((char) 0xfedf)
            + Character.toString((char) 0xfe8e); // Lam + Alef

    private final static String szLamStickAndAlef = Character
            .toString((char) 0xfee0)
            + Character.toString((char) 0xfe8e); // Lam (Sticky !!!)+
                                                    // Alef

    private final static String szLa = Character.toString((char) 0xfefb); // La
    private final static String szLaStick = Character.toString((char) 0xfefc); // La
                                                                                // (Sticky!!!)

    private final static String szLamAndAlefWoosim = Character
            .toString((char) 0xe1)
            + Character.toString((char) 0xbb); // Lam + Alef

    private final static String szLamStickAndAlefWoosim = Character
            .toString((char) 0x90)
            + Character.toString((char) 0xbb); // Lam (Sticky !!!)+
                                                // Alef

    private final static String szLaWoosim = Character.toString((char) 0xd9); // La
    private final static String szLaStickWoosim = Character
            .toString((char) 0xd9); // La

    // (Sticky!!!)

    private static final class struc {
        public char character;
        public char endGlyph;
        public char iniGlyph;
        public char midGlyph;
        public char isoGlyph;

        public struc(char Character, char EndGlyph, char IniGlyph,
                char MidGlyph, char IsoGlyph) {
            character = Character;
            endGlyph = EndGlyph;
            iniGlyph = IniGlyph;
            midGlyph = MidGlyph;
            isoGlyph = IsoGlyph;
        }
    }

    static struc[] arrStruc = {
            new struc((char) 0x630, (char) 0xfeac, (char) 0xfeab,
                    (char) 0xfeac, (char) 0xfeab),
            new struc((char) 0x62f, (char) 0xfeaa, (char) 0xfea9,
                    (char) 0xfeaa, (char) 0xfea9),
            new struc((char) 0x62c, (char) 0xfe9e, (char) 0xfe9f,
                    (char) 0xfea0, (char) 0xfe9d),
            new struc((char) 0x62d, (char) 0xfea2, (char) 0xfea3,
                    (char) 0xfea4, (char) 0xfea1),
            new struc((char) 0x62e, (char) 0xfea6, (char) 0xfea7,
                    (char) 0xfea8, (char) 0xfea5),
            new struc((char) 0x647, (char) 0xfeea, (char) 0xfeeb,
                    (char) 0xfeec, (char) 0xfee9),
            new struc((char) 0x639, (char) 0xfeca, (char) 0xfecb,
                    (char) 0xfecc, (char) 0xfec9),
            new struc((char) 0x63a, (char) 0xfece, (char) 0xfecf,
                    (char) 0xfed0, (char) 0xfecd),
            new struc((char) 0x641, (char) 0xfed2, (char) 0xfed3,
                    (char) 0xfed4, (char) 0xfed1),
            new struc((char) 0x642, (char) 0xfed6, (char) 0xfed7,
                    (char) 0xfed8, (char) 0xfed5),
            new struc((char) 0x62b, (char) 0xfe9a, (char) 0xfe9b,
                    (char) 0xfe9c, (char) 0xfe99),
            new struc((char) 0x635, (char) 0xfeba, (char) 0xfebb,
                    (char) 0xfebc, (char) 0xfeb9),
            new struc((char) 0x636, (char) 0xfebe, (char) 0xfebf,
                    (char) 0xfec0, (char) 0xfebd),
            new struc((char) 0x637, (char) 0xfec2, (char) 0xfec3,
                    (char) 0xfec4, (char) 0xfec1),
            new struc((char) 0x643, (char) 0xfeda, (char) 0xfedb,
                    (char) 0xfedc, (char) 0xfed9),
            new struc((char) 0x645, (char) 0xfee2, (char) 0xfee3,
                    (char) 0xfee4, (char) 0xfee1),
            new struc((char) 0x646, (char) 0xfee6, (char) 0xfee7,
                    (char) 0xfee8, (char) 0xfee5),
            new struc((char) 0x62a, (char) 0xfe96, (char) 0xfe97,
                    (char) 0xfe98, (char) 0xfe95),
            new struc((char) 0x627, (char) 0xfe8e, (char) 0xfe8d,
                    (char) 0xfe8e, (char) 0xfe8d),
            new struc((char) 0x644, (char) 0xfede, (char) 0xfedf,
                    (char) 0xfee0, (char) 0xfedd),
            new struc((char) 0x628, (char) 0xfe90, (char) 0xfe91,
                    (char) 0xfe92, (char) 0xfe8f),
            new struc((char) 0x64a, (char) 0xfef2, (char) 0xfef3,
                    (char) 0xfef4, (char) 0xfef1),
            new struc((char) 0x633, (char) 0xfeb2, (char) 0xfeb3,
                    (char) 0xfeb4, (char) 0xfeb1),
            new struc((char) 0x634, (char) 0xfeb6, (char) 0xfeb7,
                    (char) 0xfeb8, (char) 0xfeb5),
            new struc((char) 0x638, (char) 0xfec6, (char) 0xfec7,
                    (char) 0xfec8, (char) 0xfec5),
            new struc((char) 0x632, (char) 0xfeb0, (char) 0xfeaf,
                    (char) 0xfeb0, (char) 0xfeaf),
            new struc((char) 0x648, (char) 0xfeee, (char) 0xfeed,
                    (char) 0xfeee, (char) 0xfeed),
            new struc((char) 0x629, (char) 0xfe94, (char) 0xfe93,
                    (char) 0xfe93, (char) 0xfe93),
            new struc((char) 0x649, (char) 0xfef0, (char) 0xfeef,
                    (char) 0xfef0, (char) 0xfeef),
            new struc((char) 0x631, (char) 0xfeae, (char) 0xfead,
                    (char) 0xfeae, (char) 0xfead),
            new struc((char) 0x624, (char) 0xfe86, (char) 0xfe85,
                    (char) 0xfe86, (char) 0xfe85),
            new struc((char) 0x621, (char) 0xfe80, (char) 0xfe80,
                    (char) 0xfe80, (char) 0xfe80),
            new struc((char) 0x626, (char) 0xfe8a, (char) 0xfe8b,
                    (char) 0xfe8c, (char) 0xfe89),
            new struc((char) 0x623, (char) 0xfe84, (char) 0xfe83,
                    (char) 0xfe84, (char) 0xfe83),
            new struc((char) 0x622, (char) 0xfe82, (char) 0xfe81,
                    (char) 0xfe82, (char) 0xfe81),
            new struc((char) 0x625, (char) 0xfe88, (char) 0xfe87,
                    (char) 0xfe88, (char) 0xfe87),
            new struc((char) 0x67e, (char) 0xfb57, (char) 0xfb58,
                    (char) 0xfb59, (char) 0xfb56), // peh
            new struc((char) 0x686, (char) 0xfb7b, (char) 0xfb7c,
                    (char) 0xfb7d, (char) 0xfb7a), // cheh
            new struc((char) 0x698, (char) 0xfb8b, (char) 0xfb8a,
                    (char) 0xfb8b, (char) 0xfb8a), // jeh
            new struc((char) 0x6a9, (char) 0xfb8f, (char) 0xfb90,
                    (char) 0xfb91, (char) 0xfb8e), // keheh
            new struc((char) 0x6af, (char) 0xfb93, (char) 0xfb94,
                    (char) 0xfb95, (char) 0xfb92), // gaf
            // new struc((char) 0x6cc, (char) 0xfbfd, (char) 0xfbfe,
            // (char) 0xfbff, (char) 0xfbfc), // Farsi yeh
            new struc((char) 0x6cc, (char) 0xfbfd, (char) 0xfef3,
                    (char) 0xfef4, (char) 0xfbfc), // Arabic yeh
            new struc((char) 0x6c0, (char) 0xfba5, (char) 0xfba4,
                    (char) 0xfba5, (char) 0xfba4) // heh with yeh
    };

    static struc[] arrStrucWoosim = {
            new struc((char) 0x630, (char) 0xb5, (char) 0x82, (char) 0xb5,
                    (char) 0x82),
            new struc((char) 0x62f, (char) 0xb4, (char) 0x81, (char) 0xb4,
                    (char) 0x81),
            new struc((char) 0x62c, (char) 0x9b, (char) 0xb1, (char) 0xf9,
                    (char) 0xbf),
            new struc((char) 0x62d, (char) 0x9c, (char) 0xb2, (char) 0xfa,
                    (char) 0xc0),
            new struc((char) 0x62e, (char) 0x9d, (char) 0xb3, (char) 0xfe,
                    (char) 0xc1),
            new struc((char) 0x647, (char) 0xac, (char) 0xe4, (char) 0x93,
                    (char) 0xd5),
            new struc((char) 0x639, (char) 0xc9, (char) 0xd3, (char) 0x8b,
                    (char) 0xa4),
            new struc((char) 0x63a, (char) 0xca, (char) 0xdd, (char) 0x8c,
                    (char) 0xa5),
            new struc((char) 0x641, (char) 0xa6, (char) 0xde, (char) 0x8d,
                    (char) 0xcc),
            new struc((char) 0x642, (char) 0xa7, (char) 0xdf, (char) 0x8e,
                    (char) 0xce),
            new struc((char) 0x62b, (char) 0xbd, (char) 0xaf, (char) 0xea,
                    (char) 0x99),
            new struc((char) 0x635, (char) 0xc4, (char) 0xc8, (char) 0x87,
                    (char) 0xa0),
            new struc((char) 0x636, (char) 0xc5, (char) 0xcb, (char) 0x88,
                    (char) 0xa1),
            new struc((char) 0x637, (char) 0xc6, (char) 0xcd, (char) 0xcd,
                    (char) 0xa2),
            new struc((char) 0x643, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8),
            new struc((char) 0x645, (char) 0xd2, (char) 0xe2, (char) 0x91,
                    (char) 0xaa),
            new struc((char) 0x646, (char) 0xd4, (char) 0xe3, (char) 0x92,
                    (char) 0xab),
            new struc((char) 0x62a, (char) 0xbd, (char) 0xaf, (char) 0xea,
                    (char) 0x99),
            new struc((char) 0x627, (char) 0xbb, (char) 0x80, (char) 0xbb,
                    (char) 0x80),
            new struc((char) 0x644, (char) 0xd1, (char) 0xe1, (char) 0x90,
                    (char) 0xa9),
            new struc((char) 0x628, (char) 0xbc, (char) 0xae, (char) 0xe9,
                    (char) 0x98),
            new struc((char) 0x64a, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc),
            new struc((char) 0x633, (char) 0xc2, (char) 0xb8, (char) 0xb8,
                    (char) 0x9e),
            new struc((char) 0x634, (char) 0xc3, (char) 0xb9, (char) 0xb9,
                    (char) 0x9f),
            new struc((char) 0x638, (char) 0xc7, (char) 0xcd, (char) 0xcd,
                    (char) 0xc7),
            new struc((char) 0x632, (char) 0xb7, (char) 0xb7, (char) 0xb7,
                    (char) 0xb7),
            new struc((char) 0x648, (char) 0x94, (char) 0x94, (char) 0x94,
                    (char) 0x94),
            new struc((char) 0x629, (char) 0xda, (char) 0xda, (char) 0xda,
                    (char) 0xda),
            new struc((char) 0x649, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc),
            new struc((char) 0x631, (char) 0xb6, (char) 0xb6, (char) 0xb6,
                    (char) 0xb6),
            new struc((char) 0x624, (char) 0xe7, (char) 0xe7, (char) 0xe7,
                    (char) 0xe7),
            new struc((char) 0x621, (char) 0xba, (char) 0xba, (char) 0xba,
                    (char) 0xba),
            new struc((char) 0x626, (char) 0xd7, (char) 0xe8, (char) 0x97,
                    (char) 0xd7),
            new struc((char) 0x623, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x622, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x625, (char) 0x80, (char) 0x80, (char) 0x80,
                    (char) 0x80),
            new struc((char) 0x67e, (char) 0xbc, (char) 0xae, (char) 0xe9,
                    (char) 0x98), // peh
            new struc((char) 0x686, (char) 0x9b, (char) 0xb1, (char) 0xf9,
                    (char) 0xbf), // cheh
            new struc((char) 0x698, (char) 0xb7, (char) 0xb7, (char) 0xb7,
                    (char) 0xb7), // jeh
            new struc((char) 0x6a9, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8), // keheh
            new struc((char) 0x6af, (char) 0xcf, (char) 0xe0, (char) 0x8f,
                    (char) 0xa8), // gaf
            new struc((char) 0x6cc, (char) 0xdc, (char) 0xe6, (char) 0x95,
                    (char) 0xdc), // yeh
            new struc((char) 0x6c0, (char) 0xac, (char) 0xe4, (char) 0x93,
                    (char) 0xd5) // heh with yeh
    };

    private static final int N_DISTINCT_CHARACTERS = 43;

    private static final String ArabicReverse(String s) {
        try {
            String Out = "", rev;
            s = MakeReverse(s);
            char[] chs = new char[s.length()];
            chs = s.toCharArray();
            int i = 0;
            while (i < s.length()) {
                if ((chs[i] >= '0' && chs[i] <= '9')) // isDigit(s[i]) ?
                {
                    rev = "";
                    while (i < s.length()
                            && ((chs[i] >= '0' && chs[i] <= '9') || chs[i] == '/')) // isDigit(s[i])
                                                                                    // ?
                    {
                        rev = rev + chs[i];
                        ++i;
                    }
                    rev = MakeReverse(rev);
                    Out = Out + rev;
                } else {
                    Out = Out + chs[i];
                    ++i;
                }
            }
            s = Out;
        } catch (Exception ex) {
            // throw new Exception(
            // "An exception has occurred in ArabicReverse function.\\n"
            // + ex.getMessage());
        }
        return s;
    }

    private static final boolean isFromTheSet1(/* WCHAR */char ch) {
        char[] theSet1 = new char[] { (char) 0x62c, (char) 0x62d, (char) 0x62e,
                (char) 0x647, (char) 0x639, (char) 0x63a, (char) 0x641,
                (char) 0x642, (char) 0x62b, (char) 0x635, (char) 0x636,
                (char) 0x637, (char) 0x643, (char) 0x645, (char) 0x646,
                (char) 0x62a, (char) 0x644, (char) 0x628, (char) 0x64a,
                (char) 0x633, (char) 0x634, (char) 0x638, (char) 0x67e,
                (char) 0x686, (char) 0x6a9, (char) 0x6af, (char) 0x6cc,
                (char) 0x626 };
        int i = 0;
        while (i < 28) {
            if (ch == theSet1[i])
                return true;
            ++i;
        }
        return false;
    }

    private static final boolean isFromTheSet2(/* WCHAR */char ch) {
        char[] theSet2 = new char[] { (char) 0x627, (char) 0x623, (char) 0x625,
                (char) 0x622, (char) 0x62f, (char) 0x630, (char) 0x631,
                (char) 0x632, (char) 0x648, (char) 0x624, (char) 0x629,
                (char) 0x649, (char) 0x698, (char) 0x6c0 };
        int i = 0;
        while (i < 14) {
            if (ch == theSet2[i])
                return true;
            ++i;
        }
        return false;
    }

    private static final String MakeReverse(String text) {
        String Result = "";
        char[] Ctext = new char[text.length()];
        Ctext = text.toCharArray();
        for (int i = (text.length()) - 1; i >= 0; i--) {
            Result += Ctext[i];
        }
        return Result;
    }

    public static final String ConvertBackToRealFarsi(String In) {

        if (!isFarsiConversionNeeded) {
            return In;
        }

        String strOut = "";
        StringBuilder strBuilder = new StringBuilder("");
        int i = 0;
        int j = 0;
        char[] chIn = new char[In.length()];
        chIn = In.toCharArray();

        for (i = 0; i < In.length(); i++) {
            boolean found = false;
            for (j = 0; j < arrStruc.length; j++) {
                if (chIn[i] == arrStruc[j].midGlyph ||
                        chIn[i] == arrStruc[j].iniGlyph ||
                        chIn[i] == arrStruc[j].endGlyph ||
                        chIn[i] == arrStruc[j].isoGlyph) {
                    strBuilder.append(arrStruc[j].character);
                    found = true;
                    break;
                }
            }
            if (!found)
                strBuilder.append(chIn[i]);
        }

        strOut = strBuilder.toString();
    strOut = strOut.replace(szLa, "لا");
    strOut = strOut.replace(szLaStick, "لا");

        return strOut;
    }

    public static final String Convert(String In) {

        if (!isFarsiConversionNeeded) {
            return In;
        }

        if (In == null) {
            return "";
        }

        boolean linkBefore, linkAfter;
        String Out = In;
        char[] chOut = new char[Out.length()];
        chOut = Out.toCharArray();
        char[] chIn = new char[In.length()];
        chIn = In.toCharArray();

        for (int i = 0; i < In.length(); i++) {
            /* WCHAR */
            char ch = chIn[i];
            if ((ch >= (char) 0x0621 && ch <= (char) 0x064a)
                    || (ch == (char) 0x067e) || (ch == (char) 0x0686)
                    || (ch == (char) 0x0698) || (ch == (char) 0x06a9)
                    || (ch == (char) 0x06af) || (ch == (char) 0x06cc)
                    || (ch == (char) 0x06c0)) // is a Farsi character?
            {
                int idx = 0;
                while (idx < N_DISTINCT_CHARACTERS) {
                    if (arrStruc[idx].character == chIn[i])
                        break;
                    ++idx;
                }

                if (i == In.length() - 1)
                    linkAfter = false;
                else
                    linkAfter = (isFromTheSet1(chIn[i + 1]) || isFromTheSet2(chIn[i + 1]));
                if (i == 0)
                    linkBefore = false;
                else
                    linkBefore = isFromTheSet1(chIn[i - 1]);
                if (idx < N_DISTINCT_CHARACTERS) {
                    if (linkBefore && linkAfter)
                        chOut[i] = arrStruc[idx].midGlyph;
                    if (linkBefore && !linkAfter)
                        chOut[i] = arrStruc[idx].endGlyph;
                    if (!linkBefore && linkAfter)
                        chOut[i] = arrStruc[idx].iniGlyph;
                    if (!linkBefore && !linkAfter)
                        chOut[i] = arrStruc[idx].isoGlyph;
                } else {
                    chOut[i] = chIn[i];
                }
            } else {
                chOut[i] = chIn[i];
            }
        }
        Out = "";
        for (int j = 0; j < chOut.length; j++)
            Out += chOut[j];
        // Out = ArabicReverse(Out);

        Out = Out.replace((char) 0x200c, ' '); // Change NO SPACE to SPACE

        Out = Out.replace(szLamAndAlef, szLa); // Join 'Lam' and 'Alef' and
                                                // make 'La'
        Out = Out.replace(szLamStickAndAlef, szLaStick); // Join 'Lam Stick'
                                                            // and 'Alef'
                                                            // and make 'La
                                                            // Stick'

        return reorderWords(Out);

    }

    private final static String reorderWords(String strIn)
    {

        final int ST_RTL = 0;
        final int ST_LTR = 1;

        String strOut = "";
        String prevWord = "";
        int state = ST_RTL;
        char[] arr = strIn.toCharArray();
        int i = 0;
        while (i < arr.length) {
            if (charIsLTR(arr[i]) && state != ST_LTR)
            {
                // state changed to LTR
                state = ST_LTR;
                strOut = prevWord + strOut;
                prevWord = "";
                prevWord += arr[i];
            }
            else if (charIsRTL(arr[i]) && state != ST_RTL)
            {
                // state changed to RTL
                state = ST_RTL;
                strOut = prevWord + strOut;
                prevWord = "";
                prevWord += arr[i];
            }
            else
            {
                // state is not changed
                prevWord += arr[i];
            }
            i++;
        }

        strOut = prevWord + strOut;

        return strOut;

    }

    private final static boolean charIsLTR(char ch)
    {
        return (ch >= (char) 65 & ch <= (char) 122)
                |
                Character.isDigit(ch);
    }

    private final static boolean charIsRTL(char ch)
    {
        return ch >= (char) 0x0621;
    }   

    private static Typeface typeface;

    public static final Typeface GetFarsiFont(Context context) {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(context.getAssets(),
                    // "DroidSansFallback.ttf");
                    "TAHOMA.TTF");
        }
        return typeface;
    }
}

3) And in your Activity:

Typeface tf = Farsi.GetFarsiFont(this);
MyTextView.setTypeface(tf);
MyTextView.setText(Farsi.Convert("سلام"));

enjoy!

my reference for Farsi class: Arabizer

Bob
  • 22,810
  • 38
  • 143
  • 225
  • 3
    please do not add lots of "follow this link" posts as answers on other questions; a link by itself is not really an answer. I have converted them to comments, so they will still show as related, but without the need for empty answers. – Marc Gravell Nov 01 '11 at 09:37
  • Dear breceivemail, thanks for your solution. I used it, and everything works fine, but some users complain that the "پ" letter is not displayed correctly. I used BYekan.ttf font, not Tahoma. Does this problem is related to the font? Your help would be appreciated. I'm really confused. The other reshapers such as DariGlyphUtils.java had also the same problem. Thanks. – Farid Ala Jun 11 '12 at 07:51
  • I had this problem on Android 3.x. I asked this question [at this link](http://stackoverflow.com/q/7426266/779408) and nobody answered it. I have to control my user's device if is was tablet I stop this algorithm because Android 3.x supports unicode itself. If you found its answer please insert it in mentioned question. – Bob Jun 11 '12 at 08:25
  • There are a lot of Persian applications available on the market that work fine even on Android 2.1+. I really wonder how they have implemented reshaping. What do you do to run your applications on Android 2.1+? Do you ignore all devices with Android lower than 2.2? I'm really confused, more efforts, less result ! Thanks. – Farid Ala Jun 11 '12 at 08:51
  • This algorithm works fine on all Android versions except Android 3.x. I only ignore 3.x devices that do not need reshaping. – Bob Jun 11 '12 at 09:06
  • it does not have problem with 2.x – Bob Jun 11 '12 at 09:07
  • It renders "پ" successfully on all 2.x devices? It's strange, I used "Farsi" class with "BYekan.ttf" font, some users have problems with "پ" which is displayed like "~" . So, I conclude this is font issue. Right ? – Farid Ala Jun 11 '12 at 09:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12380/discussion-between-farid-ala-and-breceivemail) – Farid Ala Jun 11 '12 at 09:37
  • i am using android 2.2 , and i create a class Farsi in src folder , and tried to print the data comming from server , but it doesn't print arabic, i used mytextview(Farsi.convert("سلام")) but it prints english strange chars why please? – William Kinaan Jun 22 '12 at 14:32
  • @breceivemail thank you , it works good when i used this `info.setText(Farsi.Convert("سلام"));` but when i want to print data coming from server it doesn't work , please would you help me – William Kinaan Jun 22 '12 at 15:22
  • @FaridAla thank you , it works good when i used this info.setText(Farsi.Convert("سلام")); but when i want to print data coming from server it doesn't work , please would you help me – William Kinaan Jun 22 '12 at 15:25
  • @FaridAla and when i used this `Typeface tf = Farsi.GetFarsiFont(this); info.setTypeface(tf);` i got exception – William Kinaan Jun 26 '12 at 16:47
  • What about 2.3.0 - 2.3.7? does android provide full support there? – amit Aug 21 '12 at 06:42
  • I think Android 2.3.x has Arabic font but does not show your word correctly. – Bob Aug 21 '12 at 06:55
  • 1
    @totti roma: you are getting Exception Because "tahoma.ttf" Written In Caps in that Code and Your File name mighrt not be in caps. Change this and try again – Zeeshan Chaudhry Sep 14 '12 at 06:39
  • 1
    Thanks a lot, it saves a lot of time while I am searching for ROM with Arabic support – Ashraf Sayied-Ahmad Oct 01 '12 at 00:09
  • What's the conclusion? Is PEH problem solved? I read all comments, but I couldn't find the solution. – Behzad Nov 25 '12 at 09:32
  • I use webview in my app, is there any way to correct this in webview? – Ali Dec 01 '12 at 13:15
  • I am getting some html which contains a lot of Arabic sentences. Your solution is working fine but it is taking a lot time to convert and reorder the text. Is there any improvement that I can add to make it faster? – Khawar Raza Dec 26 '12 at 08:47
  • @KhawarRaza No unfortunately I have not encountered large texts and have not thought about a solution for it. I would be pleased of any suggestion for improving the performance of the algorithm. – Bob Dec 26 '12 at 09:22
  • @breceivemail, is there any way I can apply this to all the strings in strings.xml file at one go, rather than doing it one by one? My app has lots of arabic strings. – input Dec 28 '12 at 21:07
  • @input If you have constant texts inserted in `strings.xml` you can get your texts from this algorithm in another application and copy those converted texts to `strings.xml`. In this situation you do not need to convert these texts in your application at run time. – Bob Dec 29 '12 at 05:47
  • @breceivemail, please bear with me. I don't understand. How do I get the texts from the algorithm in another application and copy them to `strings.xml`? – input Dec 29 '12 at 13:36
  • @input easily you can implement it (text converter) in a windows application or you can use Android debugger to copy the string result of `Farsi.Convert` – Bob Dec 29 '12 at 19:28
  • @breceivemail It works fine with only one usage, but when I try to use (Farsi.convert("")) more than once an Exception happen..Help please – Sami Eltamawy Apr 18 '13 at 23:41
  • @AbdEl-RahmanEl-Tamawy please explain more about your exception – Bob Apr 20 '13 at 07:08
  • 1
    @breceivemail .. I was having an exception when I have more than two TextView in one Layout. I used another library and solved the problem https://github.com/agawish/Better-Arabic-Reshaper/commits/master – Sami Eltamawy Apr 20 '13 at 15:52
  • @AbdEl-RahmanEl-Tamawy: do u know any example code for using Gawish's class? I got the class from the link u posted but I can't figure out how to use it..Thanks in advance! – CSawy Jul 14 '13 at 20:05
  • @user1763553.. you have to import the class into your project and ArabicUtilities.reshapeSentence("كلام عربى") – Sami Eltamawy Jul 21 '13 at 23:51
  • This works perfectly fine except for Farsi letter پ which does not exist in Arabic. Note that this is only the case with Android 3.1 (have not tested 3.0 and 3.2) in which پ is replaced with ~ . So be careful if u are a Persian app developer :) – In order to solve the problem for Android 3.x replace the first if clause in Farsi.Convert(String ) method with this: if (!isFarsiConversionNeeded || android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) – Fatima Aug 31 '14 at 10:47
  • 1
    @Tina Yes I think it is a bug. I have asked about it in this link: http://stackoverflow.com/q/7426266/779408 – Bob Aug 31 '14 at 10:56
  • Tried using this approach, but with large Arabic files on an Android 2.2 phone it took over 20 seconds to render a page. Looking in the debugger the Garbage Collection routine was recovering huge numbers of objects and large chunks of memory several times a second – QuantumTiger Dec 19 '14 at 10:26
  • @QuantumTiger If you are going to use it for a large text, you have to optimize it and reduce the order of the algorithm yourself. – Bob Dec 20 '14 at 06:50
  • i tried to read arabic language in android studio but it not support arabic language . So which UTF have to use for support arabic language in studio? – Anand Savjani Sep 01 '15 at 05:52
8

I would like to add my experience after struggling several weeks with Arabic, Actually this post was veryhelpful me when I use arabic fonts in my android application.

I used farsi class to rearrang text. But it is not up to my expectations. because, customer is always too much consider about every letter of the application and it is arrangment.

In the farsi class, one problem is it can't rearrange closing brackets properly. Like that I noticed several issues.

However when I use ArabicUtility class, I noticed that it has improved text rendering better than Farsi class. https://github.com/agawish/Better-Arabic-Reshaper/commits/master

please not that you have to install fonts in assests folder and you can change font type from this class. Additionally I have added a new method to above class in order to get text from string filles and set text to textviews.

here is the code for that.

public void addTranslate(int rid, TextView txt1, Context context) {
        String textv = context.getResources().getString(rid);
        txt1.setText(ArabicUtilities.reshapeSentence(textv));


    }

using above method in your activities, you just have to pass above three parameters.

newday
  • 3,842
  • 10
  • 54
  • 79
3

try to copy any arabic font into assets folder and set that font and check may be it works

string[] t={"arabic words here"};
TextView tv=(TextView)findViewById(R.id.text); 
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face)

use the arabic font insted of HandmadeTypewriter.ttf font or

try to set the static arabic text and check its working or not ?

string[] t={"arabic words here"};
Textview tv=(Textview)findviewByid(R.id.text);
tv.setText(t[0]);
dilipkaklotar
  • 1,469
  • 3
  • 20
  • 28
-1

tv.setTypeFace and set the proper ttf for the arabic

n1ckolas
  • 4,380
  • 3
  • 37
  • 43
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47