2

I know there are quite some questions about this topic already but I simply can't get it to work.

What I want to achieve is the following:

A android phone no matter what language is used starts my Application. From that point the locale is overwritten by "en". When the user is closes the application the locale is set to the default again.

I've created the most basic application with a simple debug toast. My phone is in other language than "en" but after I start the application I still get my default value and NOT the value "en".

What is it that I'm doing wrong?

Code:

package com.locale.test;

import java.util.Locale;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.Toast;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Locale locale = null;
        Configuration config =null;
        config = getBaseContext().getResources().getConfiguration();
        locale = Locale.ENGLISH;
        Locale.setDefault(locale);
        config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

    /***********************************************************************************************
     * Debug
     **********************************************************************************************/

    @Override
    public boolean onSearchRequested() {
        String density;
        String orientation;

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){ density = "HDPI"; } else
        if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){ density = "MDPI"; } else
        if(metrics.densityDpi == DisplayMetrics.DENSITY_LOW){ density = "LDPI"; } else
        { density = "DEFAULT"; };

        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            orientation = "Portrait";
        } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            orientation = "Landscape";
        } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_SQUARE){
            orientation = "Square";
        } else {
            orientation = "Undefined";
        }

        Toast.makeText(this, "Density = " + density + "\n" +
                             "Orientation = " + orientation + "\n" +
                             "Width = " + metrics.widthPixels + "\n" +
                             "Height = " + metrics.heightPixels + "\n" +
                             "Locale = " + Locale.getDefault(), Toast.LENGTH_LONG).show();      
        return super.onSearchRequested();
    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.locale.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:configChanges="locale">
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Update 19 January 2012:

Added:

getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

Changed locale to:

 locale = Locale.ENGLISH;
patrick
  • 1,282
  • 4
  • 21
  • 37

2 Answers2

1

I think you should not try to do this in onResume method. I have just tested it in onCreate method (before setting layout setContentView(int)) and it works well. Moreover it should work just fine with screen orientation changing. The code looks like below:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Configuration configuration = new Configuration();
    configuration.locale = Locale.FRANCE;
    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());

    setContentView(R.layout.main);
}

i have added android:configChanges="locale" for in AndroidManifest.xml file and created values-fr folder in res.

  • Don't create a new Configuration object from scratch, you will override the user's fontScale selection in Android settings and your app fonts won't scale automatically. Instead, use Context.getResources().getConfiguration() and modify its locale. – Elad Nava Aug 23 '14 at 16:56
1

I think you forgot to call updateConfiguration() on the Resources object.

Also I don't think it matters but I'd use the Locale.ENGLISH constant.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Hi Sean, thanks for your anwers, but after adding updateConfiguration() it is still not working. See the question post for the updated code. Do you have any more suggestions? Thanks! – patrick Jan 19 '12 at 16:27
  • 1
    I found out that it does work but only after rotating the screen. So It looks like it is changed back after the app is started, but not after rotating the screen.. – patrick Jan 19 '12 at 16:52