Oct 13, 2012

Setting the locale of your application from code

Hi folks,

The built-in localization system of the Android is pretty straightforward, but sometimes you will need to change the locale of your app without changing the global locale settings on the device. If you really need this, the process is easy. Just create an Application subclass and load a locale code from somewhere (from the app preferences, from the device location, etc.):
public class LocalizedApplication extends Application {

    @Override
    public void onCreate() {
        initLocalization();
        super.onCreate();
    }
    
    private void initLocalization() {
        // Load a locale with specifying an IETF language
        // code, the short one:
        String languageToLoad = "en";
        // or the country-specific:
        String languageToLoad = "en_US";
        Locale locale = new Locale(languageToLoad);
        // or load a Locale directly:
        Locale locale = Locale.US;
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources()
            .updateConfiguration(config, getBaseContext().
                getResources().getDisplayMetrics());
    }
}
You have to specify in the manifest xml that you using your own application subclass:
<application
        android:debuggable="true"
        android:icon="@drawable/launcher"
        android:label="@string/app_name"
        android:name=".base.LocalizedApplication">

        <!-- other manifest stuff goes here -->

</application>


A word of advice: this should be considered only as a final solution, if your app logic (or your client) really needs to manage the locale settings by itself. While this is a perfectly working and correct solution, changing the locale in this way goes against the common Android practice, and it can confuse the users of your  application.

Using the Nexus S with Windows 8

Hi folks,

recently I upgraded my personal notebook running Windows 7 to Windows 8 64-bit RTM. There was one big setback that the system did not recognize my Nexus S device as an ADB interface, so I could not use it for development. Here are the steps to bring it back to life:

Open Device Manager, then find your device in the Other devices group. Right click on it, then select the Upgrade driver software option.


Automatic search will not work (otherwise it would do the trick the first time you connected your phone to the computer), so select the second option.


Because I upgraded from Windows 7, the needed drivers are already in the system, but somehow the Windows 8 will not use them unless you tell it so. Select the second option, and on the next screen select ADB Interface.



If you installed previously the Google USB drivers from the Android SDK, there could be a lot more options in this screen. In this case browse for the Samsung drivers, and select the most recent.


That's all, happy debugging! :-)