I am trying to change the language of my Android app with no success. I have:
/res/
. values/
. . string.xml
. . values-es/
. . . strings.xml
Both strings files contains the same but the content is translated according to the language code. The default language is english but I want to do some tests and use the spanish language.
I use the code written in this previous question: Change app language programmatically in Android
Here is my LoginActivity(launcher)'s onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("es");
res.updateConfiguration(conf, dm);
TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
Button mainMenu = (Button) findViewById(R.id.btnLogin);
// Listening to register new account link
mainMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), MainMenuActivity.class);
startActivity(i);
}
});
}
My target is Android 4.4.
However, this is not working on my Nexus 4 - Android 4.4, it always uses English language.