31

Possible Duplicate:
Change language programatically in Android

I am new to Android. In my application user can select a language from three languages. Based on the language selected by user, the entire application's language should be change. How can I do this?

1
  • Use this to change the language by programmatically: code' Locale locale = new Locale("en_US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext().getResources().updateConfiguration(config, null); setContentView(R.layout.activity_main); code' Commented Jun 24, 2016 at 14:21

2 Answers 2

51

Use this to change the language programmatically:

Locale locale = new Locale("en_US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext().getResources().updateConfiguration(config, null); 

Write the country code of the language in place of "en_US" for whatever language you want. For example, for Japanese, ja_JP; for Arabic, ar. Check this link for a list.

And make a folder in res/values-ja for Japanese or res/values-ar for Arabic..

And make a string.xml file, and put whatever languages you want on your layout. It will fetch the default language from values folder otherwise if you want it manually, then it will fetch from your external folder values-ar, etc.

An example of res/values-ar for Arabic:

<?xml version="1.0" encoding="UTF-8"?> <resources> <string name="label">حسب</string> <string name="name">بحث</string> <string name="search">بحث :</string> </resource> 
Sign up to request clarification or add additional context in comments.

5 Comments

where to call your code !?
Call it where you need.
will calling it one time be enough to apply it to the whole app ? or each and every activity ?
thanks your code works for me, changes the language of my app. But I also want to change language of keyboard according to that, which is not changed, can you help me?
The link for the list of languages is deprecated. Here's the latest list can be found. stackoverflow.com/a/39410286/3145960
6

You can set the locale.

 Resources res = context.getResources(); // Change locale settings in the app. DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.locale = new Locale(language_code.toLowerCase()); res.updateConfiguration(conf, dm); 

If you have language specific content - you can change that base on the setting. for more detail you can see Locale and this also

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.