Click here to Skip to main content
Licence CPOL
First Posted 15 Apr 2010
Views 12,579
Bookmarked 13 times

Force Locale on Android

By | 29 Sep 2010 | Technical Blog
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
A Technical Blog article. View original blog here.[^]

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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberAlex Manolescu10:31 1 Jan '11  
GeneralRe: My vote of 5 PinmemberAdrian Vintu7:22 27 Mar '11  
GeneralThis should help a few apps PinmemberSohjSolwin2:41 20 Apr '10  
GeneralRe: This should help a few apps PinmemberAdrian Vintu2:52 20 Apr '10  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 29 Sep 2010
Article Copyright 2010 by Adrian Vintu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid