6

I am making an app in which I want to have a page where I show a language selection page. So far I've included English, Hindi, and Marathi, with English set as the default.

My question is:

  1. how to change the whole application language in Selected Language?

  2. After choose the language whenever I reopen the application its give previous chosen language?

2

2 Answers 2

7

Put your all text in String file. For each language create separate String file(Deutsch values-de/strings.xml, French values-fr/strings.xml) and while you need to change language call following function. For English language set "en" for another set corresponding key

#Kotlin

val config = resources.configuration
val locale = Locale("en")
Locale.setDefault(locale)
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)

#Android Java

Configuration config = getBaseContext().getResources().getConfiguration();
 Locale locale = new Locale("en");
 Locale.setDefault(locale);
 config.locale = locale;
 getBaseContext().getResources().updateConfiguration(config, 
            getBaseContext().getResources().getDisplayMetrics());
0
1
 String lang= "en";

 public void changeLang(String lang) {
        Configuration config = getBaseContext().getResources().getConfiguration();
        if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {

            locale = new Locale(lang);
            Locale.setDefault(locale);
            Configuration conf = new Configuration(config);
            conf.locale = locale;
            getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
        }
    }

Try out this method....This will definitely work.. As you select your preferred language, pass your selected language code in this method and this will change the language of whole application.

Not the answer you're looking for? Browse other questions tagged or ask your own question.