How to replace Arabic numbers in DatePickerDialog itself with English numbers while still using Arabic strings in Android?

85 views Asked by At

I'm using a DatePickerDialog in my code and set the Locale of the app right before using it and in onCreate of my base activity. it's working perfectly in Arabic as it uses Arabic (called Hindi format or Eastern Arabic numerals) and Arabic strings as shown in the screenshot But I need to display English numbers in the DatePickerDialog itself when the locale is Arabic while keeping the Arabic strings upon client request as the Arabic numbers isn't used in his country which where the app will operate in.

Meaning that all ٠, ١, ٢, ٣, ٤, ٥, ٦, ٧, ٨, ٩ should be replaced with 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, is this possible to achieve?

Here's my code:

BaseActivity

  public abstract class BaseActivity extends AppCompatActivity{
   
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocalizationHelper.setLocal(this);
    }
   }

LocalizationHelper.java

  public class LocalizationHelper {
    
        public static void setLocal(Context context) {
            Locale locale = new Locale(getLanguage(context), "LY");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            Resources resources = context.getResources();
            resources.updateConfiguration(config, resources.getDisplayMetrics());
        }
    
        public static String getLanguage(Context context) {
            return SharedPreferencesHelper.getLanguage(context);
        }
    }

Calling the dialog:

  public static void showDatePicker(Context context, OnDialogActionCallBack onDialogActionCallBack, DashboardFilterDate date) {
    LocalizationHelper.setLocal(context);
    final Calendar c = Calendar.getInstance();
    DatePickerDialog pickerDialog = new DatePickerDialog(context, (view, year1, month1, dayOfMonth) -> onDialogActionCallBack.onPositiveButtonClicked(dayOfMonth, month1, year1),
            date != null ? date.getYear() : c.get(Calendar.YEAR),
            date != null ? date.getMonth() : c.get(Calendar.MONTH),
            date != null ? date.getDay() : c.get(Calendar.DAY_OF_MONTH));
    pickerDialog.show();
}

enter image description here

1

There are 1 answers

1
Elfnan Sherif On

if you want to change the result to english numbers you can use this code

public static String arabicNumbersToEn(String str) {
        char[] chars = new char[str.length()];
        for(int i=0; i<str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= 0x0660 && ch <= 0x0669){ch -= 0x0660 - '0';}
            else if (ch >= 0x06f0 && ch <= 0x06F9){ ch -= 0x06f0 - '0';}
            chars[i] = ch;
        }
        return new String(chars);
    }