Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change app language programmatically in Android

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.

like image 596
hpique Avatar asked May 24 '10 20:05

hpique


People also ask

Can I change the language of a single app Android?

Currently, users can apply language settings only from the system-wide setting on the best Android phones. There are only a few apps that offer their own language settings. Once the "Panlingual" feature launches, users will no longer have to go to every app's settings menu to change the language.

How do I change the language of runtime app?

On Android, there is no official support, documentation or API to change an entire app's language at runtime. When a user opens an app, the resource framework automatically selects the resources that best match the device language settings.


1 Answers

It's possible. You can set the locale. However, I would not recommend that. We've tried it at early stages, it's basically fighting the system.

We have the same requirement for changing the language but decided to settle to the fact that UI should be same as phone UI. It was working via setting locale but was too buggy. And you have to set it every time you enter activity (each activity) from my experience. here is a code if you still need this (again, I don't recommend that)

Resources res = context.getResources(); // Change locale settings in the app. DisplayMetrics dm = res.getDisplayMetrics(); android.content.res.Configuration conf = res.getConfiguration(); conf.setLocale(new Locale(language_code.toLowerCase())); // API 17+ only. // Use conf.locale = new Locale(...) if targeting lower versions res.updateConfiguration(conf, dm); 

If you have language specific content - you can change that base on the setting.


update on 26th of march 2020

    public static void setLocale(Activity activity, String languageCode) {         Locale locale = new Locale(languageCode);         Locale.setDefault(locale);         Resources resources = activity.getResources();         Configuration config = resources.getConfiguration();         config.setLocale(locale);         resources.updateConfiguration(config, resources.getDisplayMetrics());     } 
  • NOTES: Language code cannot got '-' & must be 2 small case letter only
like image 110
Alex Volovoy Avatar answered Sep 28 '22 07:09

Alex Volovoy