How to change action bar language when language is changed in android

1.8k views Asked by At

I created a application which has privilege to change the language. I used a SharedPreference to store the changed language. Then I used it for language change.

These are my steps,

public class SaveSharedPreference {

    static final String PREF_LANGUAGE="language";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }    

    public static void setLanguage(Context context,String language){
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(PREF_LANGUAGE,language);
        editor.commit();
    }

    public static String getLaguage(Context context){
        return getSharedPreferences(context).getString(PREF_LANGUAGE,"");
    }

}

This SaveSharedPreference class help me to store and get the changed language.

Once I changed the language, I use below step to get and put changed language.

if (SaveSharedPreference.getLaguage(context).length()!=0) {

        if(SaveSharedPreference.getLaguage(context).toString().equals("si")){
             String languageToLoad = SaveSharedPreference.getLaguage(context); // your language
             Locale locale = new Locale(languageToLoad);
             Locale.setDefault(locale);
             Configuration config = new Configuration();
             config.locale = locale;
             getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
        } else {
            String languageToLoad = SaveSharedPreference.getLaguage(context).toString(); // your language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
        }
} else {    
    String languageToLoad = "en"; // your language
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
}

I put this on onResume() method.

@Override
    protected void onResume() {
        super.onResume();
}

And these are my string.xml files for languages,

values\strings.xml

<resources>
    <string name="label_dash_board_name">My dashboard</string>
</resources>

si\strings.xml

<resources>
    <string name="label_dash_board_name">දත්ත පුවරුව</string>
</resources>    

I have gained a problem after changed the language. I could change language in EditTexts, TextViews and Buttons. But I couldn't change the language in action bar.

I can see the language changed when I close and launch the application again.

Have any ideas ?

1

There are 1 answers

0
Abhishek Singh On BEST ANSWER

setTitle method may work for you---

setContentView(R.layout.activity_main);
// Activity title will be updated after the locale has changed in Runtime
setTitle(R.string.app_name);