Posts

Showing posts with the label Locale

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,......

Android Get Current Locale, Not Default

Answer : The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched . Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated. If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e. Locale current = getResources().getConfiguration().locale; You may find that this value is updated more quickly after a settings change if that is necessary for your application. Android N (Api level 24) update (no warnings): Locale getCurrentLocale(Context context){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ return context.getResources().ge...

Android Context.getResources.updateConfiguration() Deprecated

Answer : Inspired by Calligraphy, I ended up creating a context wrapper. In my case, I need to overwrite system language to provide my app users with the option of changing app language but this can be customized with any logic that you need to implement. import android.annotation.TargetApi; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Configuration; import android.os.Build; import java.util.Locale; public class MyContextWrapper extends ContextWrapper { public MyContextWrapper(Context base) { super(base); } @SuppressWarnings("deprecation") public static ContextWrapper wrap(Context context, String language) { Configuration config = context.getResources().getConfiguration(); Locale sysLocale = null; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { sysLocale = getSystemLocale(config); } else { sysL...

Changing Locale In Android Emulator

Answer : I am using the emulator with Android 4.2 + 4.3 and there is an app to set the locale called "Custom Locale". Start emulator -> Launcher -> Custom Locale => Select the desired language from the list => Confirm with e.g "Select 'de_DE'" My solution is to use preinstalled on android emulator "Custom locale" application. Simply send intent with extra language parameter to it as below: adb shell am broadcast -a com.android.intent.action.SET_LOCALE --es com.android.intent.extra.LOCALE EN More information here - prepare android emulator for UI test automation. No Need to change local, just long press on the EditText widget, on the popup menu select input method, and change to the android keyboard.