Vishnu Satheesh
Vishnu Satheesh

Reputation: 909

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()

Upvotes: 3

Views: 872

Answers (2)

Vishnu Satheesh
Vishnu Satheesh

Reputation: 909

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);
}

Upvotes: 2

Anwar Zahid
Anwar Zahid

Reputation: 257

Add this line in the manifest file.

android:configChanges="locale|orientation"

Upvotes: 0

Related Questions