Question

i have to implement an app prototype that should have the possibility to change the language inside the app. My code:

...builder.setItems(langList, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            selectedLanguage = which;
            if (selectedLanguage != -1) {
                Configuration configuration = new Configuration(getResources().getConfiguration());
                String currentLanguage = configuration.locale.getLanguage();
                switch (selectedLanguage) {
                case 0:
                    if (currentLanguage.equalsIgnoreCase("de")) {
                        configuration.locale = Locale.ENGLISH;
                    } else {
                        return;
                    }
                    break;
                case 1:
                    if (currentLanguage.equalsIgnoreCase("en")) {
                        configuration.locale = Locale.GERMAN;
                    } else {
                        return;
                    }
                    break;

                default:
                    break;
                }
                getResources().updateConfiguration(configuration, null);
                Intent intent = new Intent(getActivity(), DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
            dialog.dismiss();
        }...

and it works nice. But the problem is - as you change the display mode (portrait <=> landscape) loads the current system language (respectively strings.xml in values folder) again. has anyone already experience with it, or maybe a suggestion how could I solve this problem?

thanks in advance..

Was it helpful?

Solution

you can when the user select its language save it in preferences and in oncreate method check if preferences have value for language or not if not load the default language

by default the change orientation will call the oncreate method so fell free to add checking code in it

OTHER TIPS

You can know problematically when the orientation change happens because the onConfigurationChanged() lifecycle event happens.

See the following example for how you can detect the orientation change. http://techblogon.com/android-screen-orientation-change-rotation-example/

When the configuration changes you can store any values you need. Please note that you can also store the values in a setting when they are first set.

When creating the activity (onCreate()) you can read the orientation and set any appropriate values.

In summary, review the Activity lifecycle, there are relevant events there you can trigger on.

so, I solved the problem using SharedPreferences:

... default:
                    break;
                }
                SharedPreferences preferences = getActivity().getSharedPreferences("SETTINGS_FILE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("currentLanguage", configuration.locale.getLanguage());
                editor.commit();

                getResources().updateConfiguration(configuration, null); ...

then I implemented method setCurrentLanguage() in my MainActivity/MainFragment:

... protected void setCurrentLanguage() {
    Configuration configuration = new Configuration(getResources().getConfiguration());
    SharedPreferences preferences = getSharedPreferences("SETTINGS_FILE", Context.MODE_PRIVATE);
    String restoredLanguage = preferences.getString("currentLanguage", null);
    if (restoredLanguage.equalsIgnoreCase("en")) {
        configuration.locale = Locale.ENGLISH;
        getResources().updateConfiguration(configuration, null);
    }
} ...

and finnaly i call this method in every activity/fragment before setContentView(...):

// in an activity
... protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setCurrentLanguage();
    setContentView(R.layout.activity_dashboard); ...
// or in a fragment
... public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setCurrentLanguage();
    View rootView = inflater.inflate(R.layout.fragment_preferences,
            container, false); ...

Thanks again for your help ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top