Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change android app language without changing phone language?

I want user to select a language inside the app. Once the language is selected, I want the strings to use the particular language.

If I change the phone language, then my app runs on the set language.

I am not able to find any way to set a language without changing the phone language. In addition, the changes should be reflected once the language is set.

Could anyone please suggest a way to do it?

like image 612
Sujit Devkar Avatar asked Feb 05 '23 23:02

Sujit Devkar


1 Answers

Try this

public static void changeLang(Context context, String lang) {
    Locale myLocale = new Locale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

Lang parameter should be "en" for English, "it" for Italian... After that you should restart your activity/fragment

like image 55
aloj Avatar answered Feb 13 '23 05:02

aloj