user782104
user782104

Reputation: 13555

How to reload the current view when I change the locale in app in android

I am currently work with changing language in the app. My app structure is tab host + fragment I have successfully change the locale but it is quite strange.

That means after I run the change locale code , it does not change the view immediately but only when I go to another tab. I think this is due to I need to reload the view, but are there any way to implment this without kill and restart the activity?

Because there is some goolge analytic code, the entry number will be increase if the user start activity again? Are there standard way to reload view? thanks

The change locale function is in one of the tabhost fragment, I have to refresh the view in tabhost (main activity), and the current fragment .

public OnClickListener setChangeLangListener(final String lang) {
    OnClickListener changeLangListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Configuration config = new Configuration(getResources()
                    .getConfiguration());

            if (lang.equals("en")) {
                config.locale = Locale.ENGLISH;
                chi.setTextColor(oldColor);
                eng.setTextColor(getActivity().getResources().getColor(android.R.color.white));
            } else {
                config.locale = Locale.TRADITIONAL_CHINESE;
                eng.setTextColor(oldColor);
                chi.setTextColor(getActivity().getResources().getColor(android.R.color.white));
            }

            getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
        }
    };
    return changeLangListener;
}

eng.setOnClickListener(setChangeLangListener("en"));
        chi.setOnClickListener(setChangeLangListener("zh"));

Upvotes: 2

Views: 4008

Answers (2)

Pararth
Pararth

Reputation: 8134

Have you tried calling setContentView. Eg.:

            String languageToLoad  = "fr"; // your language
            Locale locale = new Locale(languageToLoad); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
            activity.setContentView(R.layout.your_layout);

Upvotes: 1

SMR
SMR

Reputation: 6736

Allright add this to your manifest

android:configChanges="locale"

and override onConfigurationChanged() in your activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // refresh your views here
  super.onConfigurationChanged(newConfig);
}

go here for more info.
Hope it helps. :)

Upvotes: 1

Related Questions