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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Nikhil Shende
  • 89
  • 1
  • 1
  • 5
  • 2
    Are you familiar with [Localizing with Resources](https://developer.android.com/guide/topics/resources/localization.html) ? – Arthur Attout Mar 22 '18 at 08:08
  • https://stackoverflow.com/questions/39144710/android-font-in-more-then-one-langauge-on-single-screen/39145253#39145253 see my answer – Arjun saini Mar 22 '18 at 08:09

2 Answers2

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());
Subin Babu
  • 1,515
  • 2
  • 24
  • 50
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.