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;