Search code examples
javaandroidarabicdigits

How to convert english "abc123 " digits to arabic "ابث١٢٣ " dynamically android


UPDATED

i have been looking for solution but couldnt found it yet ..

i have tried this

// Check for language
    String tempLang = Locale.getDefault().getLanguage();
    if (tempLang == "ar") {
        Locale AR_LOCALE_EAST_NUMBERS = new Locale.Builder().setLanguageTag("ar-u-nu-arab").build();
        Locale.setDefault(AR_LOCALE_EAST_NUMBERS);

        firstName.setKeyListener(DigitsKeyListener.getInstance(getActivity().getString(R.string.input_charatars_only_valid)));
    } else {

        firstName.setKeyListener(DigitsKeyListener.getInstance(getActivity().getString(R.string.input_charatars_only_valid)));
    }
  1. first of all it gives error of API .. it says to use

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

  2. i want to use it for 15 API as my minSdk is 15

is it correct or any changes needed

UPDATED :

kindly also tell how to convert the digits in english as

"abcdefghijklmnopqrstuvwxyz123456789 " and a sapce also ""

what would be done to convert the string to arabic with abc,123 & a space ?

PS : i am new here kindly ignore is any mistake is done .. thanks


Solution

  • You can do it with replaceAll.

    First, create a method:

        public String convertToArabic(int value)
    {
        String newValue = (((((((((((value+"")
                .replaceAll("1", "١")).replaceAll("2", "٢"))
                .replaceAll("3", "٣")).replaceAll("4", "٤"))
                .replaceAll("5", "٥")).replaceAll("6", "٦"))
                .replaceAll("7", "٧")).replaceAll("8", "٨"))
                .replaceAll("9", "٩")).replaceAll("0", "٠"));
        return newValue;
    }
    

    And usage:

    String myArabicNumber= convertToArabic(123);
    Log.d("output",myArabicNumber);
    

    output:

    ١٢٣ 
    

    UPDATE:

    you can do it by concatenating them:

        String space = "\u00A0"; //space
        String myArabicCharacter = getResources().getString(R.string.help);
        String myArabicNumber= convertToArabic(123);
        String fullHomework = myArabicCharacter+space+myArabicNumber;
        Log.d("output",fullHomework);
    

    output:

         راهنما ١٢٣
    

    Update2

    YourProject/
        res/
           values/
               strings.xml
           values-ar/
               strings.xml
    
    arabic string resource:
        <string name="help">راهنما</string>
    
    english string resource:
        <string name="help">help</string>