1

    protected void onResume() {
            super.onResume();


            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
            Configuration config = getBaseContext().getResources().getConfiguration();

            String lang = settings.getString("lang_list", "");
            // Log.d("Lang",lang);
            if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
                recreate();
                Locale locale = new Locale(lang);
                Locale.setDefault(locale);
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
            }

            if (AppController.getInstance().isPreferenceChanged()) {
                setupViewPager(viewPager);
                adapter.notifyDataSetChanged();
            }
        }

I want to change the app language without recreate the app. I mean I want to do it when user select any language from the setting then it Should change the Setting Activity language without using recreate();, because when I use recreate(); it blinks the App once.

So I am not using recreate();. Instead I wrote below code in Setting Activity in AndroidManifest.xml

<activity
            android:name=".activity.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:configChanges="orientation|keyboardHidden|screenSize|locale|layoutDirection"/>

As in below screenshot you can see that I have selected "Hindi" for the language but it is not updating the Activity to Hindi. I mean "Select Country", "Select Your Language" and "Select Categories" should be display in Hindi instead of English. I have wrote String in both languages.

Can Anybody know How to change it when Change the language ? OR Why onConfigChanges is not working for locale as it is working for Orientation.

Thank you !

enter image description here

Abhishek T.
  • 1,133
  • 1
  • 17
  • 33

2 Answers2

0

In your MainActivity call sharedPrefrences on onStart callback insted of calling in onCreate

Example:-

protected void onStart() {
    SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
    String pine = sharedPreferences.getString("language", DEFAULT);
    String languageToLoad = pine;
    Locale locale = new Locale(languageToLoad);//Set Selected Locale
    Locale.setDefault(locale);//set new locale as default
    Configuration config = new Configuration();//get Configuration
    config.locale = locale;//set config locale as selected locale
    this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
    invalidateOptionsMenu();  //This changes language in OptionsMenu too
    setTitle(R.string.app_name);  //calling app name according to language selected by user
    super.onStart();
}
Queendevelopers
  • 183
  • 3
  • 20
  • I need to Change Activity Language as I select another Language....All App should automatically changed to selected language... – Abhishek T. Aug 23 '16 at 15:45
0

You can use a singleton class that will handle the language change without restarting the activity,

public class MyApplication extends Application
{
    private Locale locale = null;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        if (locale != null)
        {
            newConfig.locale = locale;
            Locale.setDefault(locale);
            getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        Configuration config = getBaseContext().getResources().getConfiguration();

        String lang = settings.getString(getString(R.string.pref_locale), "");
        if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
        {
            locale = new Locale(lang);
            Locale.setDefault(locale);
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        }
    }
}
Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50