Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

android - How can I change language of whole application by only single click?

I have a Spinner with three language choice on login page. I want when user choose one language suppose "Persian" language the language of whole application should change. I am able to change language of current Activity only. How can I change the language of whole application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change Language Programmatically in Android

Here is the appropriate way of changing the locale of the application:

There are some difficulties which you have to overcome to change the language programmatically.

1.)Your application will not remember your language change after it is closed or recreated during the configuration change.

2.)You should update currently visible UI properly according to the selected language.

Solution

LocaleHelper” is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.

After the recent changes in Android API Version 24(Nougat) we need to override attachBaseContext to reflect changes.

Below method used to change language for application :

private static boolean updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return true;
}

find more details in below link :

Changing Android’s Locale Programmatically


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...