How to force Android application to reload the activity title corresponding to the language change?

我的梦境 提交于 2020-01-24 04:19:04

问题


How could force android application to reload activity title when the language of the application has been changed? For your information, the application reloads the title only when the device is rotated. Yet, the content of the activity is reload with the appropriate language content with no problem, but the activity title!

In AndroidManifest.xml file, android:configChanges="locale" has been added with no success.

<activity
    android:name=".layout.MainActivity"
    android:configChanges="locale"
    android:label="@string/app_name" />

Even overriding onConfigurationChanged event does not work,

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

    this.setTitle(getResources().getString(R.string.app_name));
}

Thank you,


回答1:


Same problem, solved by forcing ActionBar label reload in Activity onCreate method:

    PackageManager pm = getPackageManager();
        try {
            ActivityInfo ai = pm.getActivityInfo(this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
            if (ai.labelRes != 0) {
                getSupportActionBar().setTitle(ai.labelRes);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.stacktrace();
        }



回答2:


you need to add translations for your title in the app and then the application will change your title automatically based on the user device language... here is how to add languages folders to your app How to pragmatically switch between different language string resource folders in android?



来源:https://stackoverflow.com/questions/20726377/how-to-force-android-application-to-reload-the-activity-title-corresponding-to-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!