Force Locale on Android

by adrian vintu 1/14/2010 8:51:00 PM
While playing with localization on the Android for my app I came upon an annoying bug.
It seems running on Motorola Milestone the fonts were getting smaller on each locale update.

I use the following code for locale changing (from http://almondmendoza.com/2009/01/28/force-localize-an-application-on-android/):
String languageToLoad  = "de";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
Also in the AndroidManifest.xml I have (without this entry you get "Something wrong here, didn't expect "mypackagename" to be resumed"):
<activity
  android:label="@string/app_name"
  android:name=".Dex"
  android:configChanges="locale">
Here is how my screen should look like:



And here is how it was looking on Motorola Milestone.



After changing the Locale from Preferences the fonts keep gettting smaller and smaller



This was very annoying. I did not know exactly why this was happening.

So I decided to reproduce the Milestone in the emulator. I got the specifications of the Milestone from the web and created in Eclipse an AVD with a resolution of 480x854 and density of 240.



After that I was able to reproduce the bug. It seems the density was the issue here.
I searched for Android density and after some tries I found the solution. It seems I needed to have this in the AndroidManifest.xml
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
/>
The most important is the android:anyDensity =" true ". After this, the application was shown correctly even on the Motorola Milestone.

I hope this will be of help and don't forget to check out my enhanced logcat tool here http://adrianvintu.com/blogengine/post/Colored-Logcat-Script-for-Windows.aspx


EDIT: please read the comments of the article, you may find solutions to some of your localization problems - e.g. changing orientation resets locale, etc.

EDIT 2: people have reported that sometimes the location resets to system default. Please read my post here: locale change does not affect menu

Related posts

Comments

1/30/2010 12:58:49 AM

chrisonline

Thanks a lot this solves the problem!!

Do i need to add this code on every activity or is it enough to add it in the main activity?
String languageToLoad = "de";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

And why do i need to set the Locale.setDefault? Without this it does the same.
In some forums there are problems on milestone/droid with Locale.setDefault (permission problem).

chrisonline

1/30/2010 1:06:43 AM

adrian vintu

Sure Chris, my pleasure.

I have the code in a static method in a POJO. I call it once on onCreate(of the main activity) or when the user changes the locale from my Preferences screen.

I don't know about the Locale.setDefault magic, you are going to have to test a little bit Smile

adrian vintu

1/30/2010 12:23:27 PM

chrisonline

After changing the language in the preferences screen the main activity is still in the language before.
I do close the app and the user must start the app himself.

Is there any other way to restart a app from code instead of user interaction?
How do you resolve this?

chrisonline

1/30/2010 12:37:07 PM

adrian vintu

The locale is taken into account on recreation of the Activity afaik. This means you will see the changes in the Preference activity next time you open it.

But, as you said, the main Activity still holds original locale.

In my code, I just reset all control texts - yes, very annoying (textView.setText(getFromResource(...))).

Also, the menu options are recreated all the time like:

@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
menu.clear();

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);

return true;
}

And the list context menu:

list.setOnCreateContextMenuListener(new OnCreateContextMenuListener()
{
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
menu.add(0, MENU_ITEM_ADD_TO_WORD_LIST, 0, R.string.menu_item_add_to_word_list);
menu.add(0, MENU_ITEM_COPY, 0, R.string.menu_item_copy);
}

adrian vintu

1/30/2010 2:57:00 PM

chrisonline

I've found a better solution to reload all components without "closing" the app.

In the preference screen onLanguageChange i call the method
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

This methods changes the screen orientation to landscape and afterward to SENSOR sensitive.
AND it automatically restarts the app on changing orientation, so all is tranlated in the new selected language.

In the main app onActivityResult i do the same. So the main app also gets restarted.
Works perfect for me so give it a try.

chrisonline

1/30/2010 3:12:29 PM

adrian vintu

Cool, thank you for the tip Smile

adrian vintu

2/3/2010 6:34:06 AM

adrian vintu

Of course, you always have to test which method is better. In my case resetting 3 strings and re-creating the menu options on menu press seemed far more a reasonable thing to do - from the point of view of performance and usability.

Using the screen orientation trick would be useful for a much complexer UI.

adrian vintu

2/24/2010 4:14:16 PM

cousinHub

Thanks a lot for this, I had the same issue and referred to your page for the solution ! I hope it will help many developpers : stackoverflow.com/.../android-changing-locale-within-the-app-itself

cousinHub

3/3/2010 1:04:26 PM

WarrenFaith

Hey,

any ideas why my locale is reseted to the device locale, if I change my orientation?

Hope you can help me!

WarrenFaith

3/10/2010 10:50:02 PM

adrian vintu

when you change orientation the application restarts.

you need to save the locale in the preferences and add code in the onCreate to set the locale again. it's as simple as that.

adrian vintu

11/17/2010 4:51:43 PM

pingback

Pingback from blog.ysbilgin.com

Yavuz Selim Bilgin » Android-Locale DeÄŸiÅŸtirme

blog.ysbilgin.com

9/28/2011 10:27:09 AM

pingback

Pingback from lepeng.net

Android 多语言 | androidblogger

lepeng.net

7/7/2012 10:00:13 AM

pingback

Pingback from 59n.net

Android 多语言实现 | 南龙的小站

59n.net

7/16/2012 11:55:08 PM

pingback

Pingback from roosmaa.net

Application wide locale override | Mart Roosmaa

roosmaa.net

9/19/2012 7:25:52 AM

pingback

Pingback from jisku.com

Why typeface language reverts back after some activity in android? | Jisku.com - Developers Network

jisku.com

9/29/2012 9:27:59 PM

pingback

Pingback from testingin.com

android 应用程序实现语言切换 | 软件测试站

testingin.com

7/21/2013 10:31:49 PM

pingback

Pingback from ggkf.com

Changing Locale within the app itself | Developers Q & A

ggkf.com

12/16/2013 5:35:10 AM

pingback

Pingback from jptab.com

Displayed resolution density of drawables halving on each program start | Jptab

jptab.com

Comments are closed

About Adrian Vintu

Adrian Vintu

Adrian Vintu is an old timer who has had the wonderful experience of working in a variety of quality environments and with various quality people.

Throughout time he has got down with Assembler, C++, Borland Delphi, Java, C#, Android etc. He took part in projects concerning software security, industrial controllers, food and health-care ERPS, AI simulations, data mining, mobile development, computer games, augmented reality, online distributed transactions, and financial management and trading.

In his spare time he develops free educational applications that run on Android and Windows Phone. It is his way of thanking and giving back to the open source and free (as in beer) software community and a way of bringing value to the social community and young generation.

Send mail

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008 - 2014

Sign in