Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView Numbers shown in English in Persian font

I am loading a string which contains some number (all in Persian) into an android TextView. Everything was fine until I changed my custom font, numbers of text shown as English number.

Expected : ۱۲۳۴
Received : 1234

I know that my new font support Persian number. When I change the number locale using code below the number shown correctly.

NumberFormat numberFormat = NumberFormat.getInstance(new Locale("fa", "IR"));
String newNumber = numberFormat.format(number);

The problem is I have a string and it's hard to find the numeric part and change it. also my previous font works fine and I can't understand what's the problem with this font.

Any Idea how to globally solve this problem for all textview, or at least for a string?

like image 891
Mohamad MohamadPoor Avatar asked Jan 29 '23 23:01

Mohamad MohamadPoor


2 Answers

Try to use this method:

private String setPersianNumbers(String str) {
    return str
            .replace("0", "۰")
            .replace("1", "۱")
            .replace("2", "۲")
            .replace("3", "۳")
            .replace("4", "۴")
            .replace("5", "۵")
            .replace("6", "۶")
            .replace("7", "۷")
            .replace("8", "۸")
            .replace("9", "۹");
}
like image 51
Alireza Noorali Avatar answered Jan 31 '23 11:01

Alireza Noorali


You can use this

 String NumberString = String.format("%d", NumberInteger);

123 will become ١٢٣

like image 22
Sameer Muslim Avatar answered Jan 31 '23 13:01

Sameer Muslim