Click here to Skip to main content
Click here to Skip to main content

Force Locale on Android

, 29 Sep 2010
Rate this:
Please Sign up or sign in to vote.
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-an

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 on my blog, 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  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Adrian Vintu

Austria Austria
Adrian 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, health-food and health-care ERPS, AI simulations, data mining and mobile development.
 
Adrian is always interested in the big picture of the things and wishes to become a great architect one day.
 
Adrian maintains a blog here http://adrianvintu.com[^] and is trying to make it as nice and helpful and user friendly as possible.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAlex Manolescu1-Jan-11 10:31 
GeneralRe: My vote of 5 PinmemberAdrian Vintu27-Mar-11 7:22 
GeneralThis should help a few apps PinmemberSohjSolwin20-Apr-10 2:41 
GeneralRe: This should help a few apps PinmemberAdrian Vintu20-Apr-10 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Sep 2010
Article Copyright 2010 by Adrian Vintu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid