androidandroid-layoutandroid-fragmentslocalearabic-support

How to implement localization from fragment?


I am switching application language English to Arabic and vice versa from a fragment. The code below works, it loads the values-ar\strings.xml and the language is changed from english to arabic. The app's UI elements doesn't change immediately based on the updated language settings, RTL alignment. The change is only reflected when application is closed and reopened.

Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getResources().updateConfiguration(config, getActivity().getResources().getDisplayMetrics());
getActivity().recreate()

Solution

  • It worked, add this to the activity holding the fragment and call it instead of getActivity().recreate().

    public void restartActivity() {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }