Click here to Skip to main content
15,881,173 members
Articles / Mobile Apps / Android

Force Locale on Android

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
29 Sep 2010CPOL1 min read 24.1K   13   4
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-

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)


Written By
Website Administrator none
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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