0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
omniyo
  • 330
  • 2
  • 6
  • 19
  • 1
    Does this answer your question? [Change app language programmatically in Android](https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android) – Heitor Paceli Feb 17 '22 at 02:32
  • Per-app language feature was just added to the latest Android API 33, that is still on Developer preview. See my answer at https://stackoverflow.com/a/71151685/5038317 – Heitor Paceli Feb 17 '22 at 02:33

1 Answers1

1

The values-es folder should be on the same level as the values folder. Your example above looks like you have values-es inside the values folder.

Jimmy Collins
  • 3,294
  • 5
  • 39
  • 57