Arabic Number In Arabic Text In Android


Answer :

There's such issue in Google's bugtracker: Arabic numerals in arabic language intead of Hindu-Arabic numeral system

If particularly Egypt locale doesn't work due to some customer's issue(I can understand it), then you can format your string to any other western locales. For example:

 NumberFormat nf = NumberFormat.getInstance(new Locale("en","US")); //or "nb","No" - for Norway  String sDistance = nf.format(distance);  distanceTextView.setText(String.format(getString(R.string.distance), sDistance)); 

If solution with new Locale doesn't work at all, there's an ugly workaround:

public String replaceArabicNumbers(String original) {     return original.replaceAll("١","1")                     .replaceAll("٢","2")                     .replaceAll("٣","3")                     .....; } 

(and variations around it with Unicodes matching (U+0661,U+0662,...). See more similar ideas here)

Upd1: To avoid calling formatting strings one by one everywhere, I'd suggest to create a tiny Tool method:

public final class Tools {      static NumberFormat numberFormat = NumberFormat.getInstance(new Locale("en","US"));      public static String getString(Resources resources, int stringId, Object... formatArgs) {         if (formatArgs == null || formatArgs.length == 0) {             return resources.getString(stringId, formatArgs);         }          Object[] formattedArgs = new Object[formatArgs.length];         for (int i = 0; i < formatArgs.length; i++) {             formattedArgs[i] = (formatArgs[i] instanceof Number) ?                                   numberFormat.format(formatArgs[i]) :                                   formatArgs[i];         }         return resources.getString(stringId, formattedArgs);     } }  ....  distanceText.setText(Tools.getString(getResources(), R.string.distance, 24)); 

Or to override the default TextView and handle it in setText(CharSequence text, BufferType type)

public class TextViewWithArabicDigits extends TextView {     public TextViewWithArabicDigits(Context context) {         super(context);     }      public TextViewWithArabicDigits(Context context, AttributeSet attrs) {         super(context, attrs);     }      @Override     public void setText(CharSequence text, BufferType type) {         super.setText(replaceArabicNumbers(text), type);     }      private String replaceArabicNumbers(CharSequence original) {         if (original != null) {             return original.toString().replaceAll("١","1")                     .replaceAll("٢","2")                     .replaceAll("٣","3")                     ....;         }          return null;     } } 

I hope, it helps


There is an easy way. Taking your integer number and format it to string, and it will apply localization implicitly to numbers by this method:

String YourNumberString = String.format("%d", YourNumberInteger); 

So 123 will become ١٢٣ and so on.

For more information see section "Format numbers" at: https://developer.android.com/training/basics/supporting-devices/languages


Set your TypeFace as per below for Arabic

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/abcd.TTF"); 

abcd is Arabic font.

textview.setTypeface(font); 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?