Search code examples
androidandroid-viewpagerlocalemultilingualscreen-rotation

Android - Support Multilanguage app in a ViewPager


I have a ViewPager with FragmentPagerAdapter and five tabs that is called from my MainActivity. I want to change dynamically the language of my App. To do this, i saved my language preference in my onSavedInstanceState like this:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String predefined_lang = prefs.getString(LANGUAGES, "error");
    String sub_lan = predefined_lang.substring(0,2).toLowerCase();
    outState.putString(LANG, sub_lan);
    System.out.println(">>saveinstance"+predefined_lang);
}

and than i retrieve the language from onRestoreInstanceState:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    Configuration configuration = getResources().getConfiguration();
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    //System.out.println(savedInstanceState.getString(LANG));

    //configuration.locale = new Locale(savedInstanceState.getString(LANG));
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String predefined_lang = prefs.getString(LANGUAGES, "error");
    String sub_lan = predefined_lang.substring(0,2).toLowerCase();
    configuration.locale = new Locale(savedInstanceState.getString(LANG));
    getResources().updateConfiguration(configuration, displayMetrics);
    System.out.println(">>onrestore"+savedInstanceState.getString(LANG));

}

So i try to avoid that the chosen language is not changed again in the default language of the device. Actually, all string resources are displayed in the correct language, except for the names of the tab of the viewPager. For example:

i have these five tabs:

| tab1 -|- tab2 -|- tab3 -|- tab4 -|- tab5 |

if default language of the phone is in Italian but i chose to display the app in English, everything is written in English, even the name of the tabs. But when i rotate the screen, everything remains written in English except the names of the tab that, instead, are in Italian. Solutions?


Solution

  • you can stop reloading application when orientation changes, by adding to your manifest :

    <activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
    </activity>
    

    and to your MainActivity:

     public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    
        }
    }
    

    or you can specify any other action, read more about Runtime Changes: http://developer.android.com/guide/topics/resources/runtime-changes.html

    hope this helps