7

I'm using this code below to change my app language on button click (changing from french to english for example), it's works fine on android 4.0 + but on 5.0 it doesn't.

Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);
Configuration configEn = new Configuration();
configEn.locale = localeEn;
getApplicationContext().getResources().updateConfiguration(configEn, null);
this.recreate();

Any clues why please?

edit : this is my manifest ( with android:configChanges )

<activity
            android:name=".activities.LoginActivity"
            android:configChanges="orientation|locale"
            android:label="@string/app_name"
            android:screenOrientation="portrait"/>
Ahmed Abidi
  • 1,047
  • 12
  • 24
  • You need to use your activity context and not your application context. – Duda Oct 21 '15 at 15:03
  • 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:21
  • 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

4 Answers4

13

Try to change from this:

Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);

to this

String language = "en";
String country = "US";
Locale locale = new Locale(language , country);
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
4

My solution, that i got from Udhay, works when user changes the language in actionbar and app "refreshes" with selected language. I am using android 6.0.

There is no need to add locale to androidManifest.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Locale locale = null;
    switch (item.getItemId()) {
        case R.id.action_en:
            locale = new Locale("en_US");
            Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
            break;
        case R.id.action_is:
            locale = new Locale("is", "IS");
                    Toast.makeText(this, "Íslanska", Toast.LENGTH_SHORT).show();
            break;

    }

    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, MainActivity.class);
    startActivity(refresh);
    finish();
    return true;
}
Community
  • 1
  • 1
Sindri Þór
  • 2,887
  • 3
  • 26
  • 32
1

Have you added android:configChanges="locale" in AndroidManifest.xml? I think the problem is in your AndroidManifest.xml file.

You can see example change locale on my github repository.

germi
  • 4,628
  • 1
  • 21
  • 38
mlevytskiy
  • 1,555
  • 2
  • 16
  • 26
1

My solution was changing the locale before activity's

setContentView(R.layout.layout_main); 
gofr1
  • 15,741
  • 11
  • 42
  • 52
Zohar
  • 1,820
  • 1
  • 18
  • 16