I am changing the language in MainActivity.onCreate()
like this:
Locale locale = new Locale("cs");
Configuration config = getResources().getConfiguration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
It works OK at first look, the layout is in he correct language. But when I open the menu, it is localized to the system default language (English)! To find out what's happening I checked every 30ms (via Timer
) what is the value of getResources().getConfiguration().locale.getLanguage()
. About 50ms after onResume()
is finished, the language magically changes from "cs" to "en".
I tried to set it back after I detect the change, but it was not working. What worked was if I set the language again to "cs" in onCreateOptionsMenu()
.
Important note: this happens only if the app is started after it was previously killed. By "killed" I don't mean MainActivity.finish()
is called but rather that the app is completely removed from the memory including static fields (in practice this happens after install or if the system kills it due to low memory).
UPDATE: Another issue I noticed is that when I change the orientation of the phone to landscape, my app still uses portrait layout and not the layout from layout-land folder.