1

I create a ListPreference with a few languages in setting menu of my app. I used the following code to change the language based on user choice :

findPreference("language_list").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            String loadLanguage =  (String) newValue;
            Locale locale = null;
            if (loadLanguage.equals("en")) {
                locale = new Locale("en");

            } else if (loadLanguage.equals("ku")) {
                locale = new Locale("ku");

            } else if(loadLanguage.equals("ar")){
                locale = new Locale("ar");

            }else if(loadLanguage.equals("tr")){
                locale = new Locale("tr");
            }
            else if(loadLanguage.equals("fr")){
                locale = new Locale("fr");
            }

            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getActivity().getResources().updateConfiguration(config,
                    getActivity().getResources().getDisplayMetrics());
            return true;
        }
    });

the code works but it is needed to restart the app (exit and start it again) till language change be done completely. with the above code some part of the app will remains and do not change even they do not work.

could you please give me an idea what should I do for this?

Thank you

Sir1
  • 732
  • 1
  • 8
  • 18
  • Check [this](http://stackoverflow.com/a/25175129/3831557) solution i suggested in another SO post. You can use it after applying some amendments. – Rajen Raiyarela Jun 15 '16 at 14:33
  • Out of interest, is the purpose of this app language intensive? Language settings can be set by default in the device, so just wondering if there is a reason to make language app specific. Not questioning your work, just wondering. – zgc7009 Jun 15 '16 at 14:35
  • @RajenRaiyarela it sounds good but I have implements a lot of code and it is not the best for me to change all the codes, do you think it is not any solution to complete mine and bug fix ? – Sir1 Jun 15 '16 at 14:39
  • @zgc7009 actually I am going to add Kurdish language to my app as default that is not in Android language list – Sir1 Jun 15 '16 at 14:42
  • 1
    check [this](http://stackoverflow.com/a/13862572/3831557) and also can use [recreate](https://developer.android.com/reference/android/app/Activity.html#recreate%28%29) – Rajen Raiyarela Jun 15 '16 at 14:45
  • seems a good solution, thank u, I am going to try it – Sir1 Jun 15 '16 at 14:48
  • I used recreate and it works, great Idea <3, the first link define the problem that why some part did not changed and recreate is the answer – Sir1 Jun 15 '16 at 14:52

2 Answers2

1

You can programmatically restart the app in a hacky way as answered by @Oleg Koshin here

Community
  • 1
  • 1
Zino
  • 196
  • 2
  • 15
0

You can call

recreat();

After you have done changing language.

Like this:

    findPreference("language_list").setOnPreferenceChangeListener(new OnPreferenceChangeListener() { 

    @Override public boolean onPreferenceChange(Preference preference, Object newValue) {  
String loadLanguage = (String) newValue;  
Locale locale = null; 
 if (loadLanguage.equals("en")) { 
 locale = new Locale("en"); 
 } else if (loadLanguage.equals("ku")) { 
 locale = new Locale("ku"); 
 } else if(loadLanguage.equals("ar")){ 
 locale = new Locale("ar");
  }else if(loadLanguage.equals("tr")){
  locale = new Locale("tr"); 
 } else if(loadLanguage.equals("fr")){  
locale = new Locale("fr");
  }  
Locale.setDefault(locale); 
 Configuration config = new Configuration(); 
 config.locale = locale; 
getActivity().getResources().updateConfiguration(config, getActivity().getResources().getDisplayMetrics());  
recreat();   
return true;  
}  
});