Search code examples
javaandroidfontslocaleright-to-left

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?


Solution

  • 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", "۹");
    }