1

I'm having trouble with changing the app language manually, in the app, I offer users the ability to change the app's language to their preferred, the code below works fine even in Android (Pixel 3 Emulator), but for some reason, it doesn't work on all Samsung devices

            Context context = LocaleUtils.setLocale(getApplicationContext(), languageCode);
            Resources resources = context.getResources();
            Locale myLocale = new Locale(languageCode);
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration conf = resources.getConfiguration();
            conf.locale = myLocale;
            resources.updateConfiguration(conf, dm);
            Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(
                    getBaseContext().getPackageName());
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }

Application class:

 @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        LocaleUtils.onAttach(base, Locale.getDefault().getLanguage());
        MultiDex.install(this);
   }

on each Activity:

  @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(LocaleUtils.onAttach(newBase)));
    }
  • 1
    I have the same problem. I tried multiple libraries without any success. I finally managed to get my hands on a Samsung device and I debugged that in some cases calling getResources() in an activity returns resources that have their configuration set back to the original locale. It seems there are cases where something else overrides the locale in an activity. PS: I override the locale in the onCreate method in every activity. – NewestStackOverflowUser Jan 15 '21 at 19:22
  • I just managed to fix it for the device that I am having. I ended up using this solution https://stackoverflow.com/a/59370534/3296947 and calling it in the attachBaseContext of the activity right after calling super(). More on this topic here: https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx/ – NewestStackOverflowUser Jan 15 '21 at 22:36

1 Answers1

0

I struggled with dynamic Locale change on Samsung devices even prior to Android 10.
There might be a better solution now,
but at the time in addition to what you have done,
I ended up retrieving all the strings by resource identifiers in the following way:

public static String getStringByIdentifier(final Context context, final String stringResIdName, final boolean forceRefresh) {
        final Resources res = getResources(context, forceRefresh);
        String result;
        try {
            result = res.getString(res.getIdentifier(stringResIdName, "string",
                    context.getPackageName()));
        } catch (final Resources.NotFoundException e) {
            result = stringResIdName; // TODO or here you may throw an Exception and handle it accordingly. 
        }
        return result;
    }

public static String getStringByIdentifier(final Context context, final String stringResIdName) {
        return getStringByIdentifier(context, stringResIdName, false);
    }

 private static Resources getResources(final Context context, final boolean refreshLocale) {
        if (!refreshLocale) {
            return context.getResources();
        } else {
            final Configuration configuration = new Configuration(context.getResources().getConfiguration());
            configuration.setLocale(Locale.getDefault());
            return context.createConfigurationContext(configuration).getResources();
        }
    }

So you have to set text in the next way:

textView.setText(AndroidUtils.getStringByIdentifier(context, "string_res_name"));

Where the corresponding string resource is:

<string name="string_res_name">Some string</string>
Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54