Change app language programmatically in Android

2018-12-31 00:49发布

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific language?

I would like to let the user change the language of the app from the app.

30条回答
怪性笑人.
2楼-- · 2018-12-31 01:18

Alex Volovoy answer only works for me if it's in onCreate method of the activity.

The answer that works in all the methods is in another thread

Change language programmatically in Android

Here is the adaptation of the code



    Resources standardResources = getBaseContext().getResources();

    AssetManager assets = standardResources.getAssets();

    DisplayMetrics metrics = standardResources.getDisplayMetrics();

    Configuration config = new Configuration(standardResources.getConfiguration());

    config.locale = new Locale(languageToLoad);

    Resources defaultResources = new Resources(assets, metrics, config);

Hope that it helps.

查看更多
明月照影归
3楼-- · 2018-12-31 01:19

The only solution that fully works for me is a combination of Alex Volovoy's code with application restart mechanism:

void restartApplication() {
    Intent i = new Intent(MainTabActivity.context, MagicAppRestart.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MainTabActivity.context.startActivity(i);
}


/** This activity shows nothing; instead, it restarts the android process */
public class MagicAppRestart extends Activity {
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        finish();
    }

    protected void onResume() {
        super.onResume();
        startActivityForResult(new Intent(this, MainTabActivity.class), 0);         
    }
}
查看更多
忆尘夕之涩
4楼-- · 2018-12-31 01:19
Locale locale = new Locale("en");
Locale.setDefault(locale);

Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);

Important update:

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

Note, that on SDK >= 21, you need to call 'Resources.updateConfiguration()', otherwise resources will not be updated.

查看更多
若你有天会懂
5楼-- · 2018-12-31 01:19

Locale configuration should be set in each activity before setting the content - this.setContentView(R.layout.main);

查看更多
泪湿衣
6楼-- · 2018-12-31 01:19
private void setLanguage(String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    getResources().updateConfiguration(config,
            getResources().getDisplayMetrics());

}
查看更多
十年一品温如言
7楼-- · 2018-12-31 01:21

If u write

android:configChanges="locale"

in every activity than no need to set it every time you enter activity

查看更多
登录 后发表回答