views:

100

answers:

2

I want to have a configurable language settings in my app.

So, in onCreate of my activity, I call Resources.updateConfiguration with the new locale.

However, after onCreate (at some time, I can't find it when), the locale is set back to the default locale.

On the code example below, the strings shown in the main layout (as inflated by setContentView) shows the "in" language version, BUT when I press the menu button, on which onCreateMenu is called, the strings is taken from the "en" (default) locale.

The log shows this:

 18337               oncreate  D  { scale=1.0 imsi=525/1 loc=intouch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}
 30430        ActivityManager  I  Displayed activity yuku.coba.locale/.CobaLocaleActivity: 266 ms (total 266 ms)
 18337        KeyCharacterMap  W  No keyboard for id 65540
 18337        KeyCharacterMap  W  Using default keymap: /system/usr/keychars/qwerty.kcm.bin
 18337                 onmenu  D  { scale=1.0 imsi=525/1 loc=en_GB touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}

Between "oncreate" and "onmenu", the locale magically changes.

Please help, I have been tinkering with this with no luck.

Code snippet:

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Configuration c = new Configuration();
       c.locale = new Locale("in");
       getResources().updateConfiguration(c, null);

       setContentView(R.layout.main);
       Log.d("oncreate", getResources().getConfiguration().toString());
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       Log.d("onmenu", getResources().getConfiguration().toString());
       new MenuInflater(this).inflate(R.menu.utama, menu);

       return true;
   }
A: 

I advice not force locale to something that is different from the device setting. You will run into the problems. You can move your call to onResume. It will be better, but still you're gonna fight device configuration. Again. Don't do this .

Alex Volovoy
One case is because my app contains Indonesian language, a language that is currently not available on most phones. Still, a lot of Indonesians want the application in their own language, although the phone itself doesn't support that.
yuku
I do understand the case. Unfortunately, in my experience, it doesn't work very well. I've tried to do the same some time ago, but basically gave up because of all weirdness it caused.
Alex Volovoy
Thanks, but for now I did what fhucho said, and it seems to be OK (tried in emulator 1.5, 1.6, 2.1 and my mobile 2.2). Maybe it will get some wierdness if I was using it for more complex application, but for now it works :)
yuku
+1  A: 

Every 100ms reset the Configuration :)

fhucho
Thanks. What I do now is reset after 200 ms and reset after 400 ms. After that no need to reset again. It seems to work :D
yuku
Great :) I still have one problem on my Nexus - if I change the orientation to landscape, my app doesn't change the layout to the layout I have in layout-land folder. Do you have the same problem?
fhucho
The problem with orientation was my stupid mistake - for testing purposes I put android:configChanges="orientation|locale|etc..." in the manifest, and fergot about it.
fhucho
fhucho, one more thing we need to do is onConfigurationChange, the locale seem to be reset to the default. Make sure to force it back.
yuku