Troubles with activity title language

前端 未结 4 1079
一个人的身影
一个人的身影 2021-02-18 20:26

I have two language in my app. values/strings.xml and values-ru/strings.xml When I programmatically change language, all strings translating, b

相关标签:
4条回答
  • 2021-02-18 20:39

    Adding setTitle(R.string.activity_title) in onCreate() updates the Activity title as well as the content itself on changes in locale in Runtime.

    Following is the simple snippet that updates its content and activity's title to Korea (ko-KR) when the button is clicked:

    public class MainActivity extends AppCompatActivity {
    
        Button btnReset;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Activity title will be updated after the locale has changed in Runtime
            setTitle(R.string.app_name);
    
            btnReset = (Button) findViewById(R.id.button);
            btnReset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Resources resources = getResources();
                    DisplayMetrics dm = resources.getDisplayMetrics();
                    Configuration conf = resources.getConfiguration();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        conf.setLocale(Locale.KOREA);
                    } else {
                        conf.locale = Locale.KOREA;
                    }
                    resources.updateConfiguration(conf, dm);
    
                    // Overwrite the default Locale
                    Locale.setDefault(Locale.KOREA);
    
                    // Clear the back stack then restarts the activity
                    startActivity(new Intent(MainActivity.this, MainActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK));
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-02-18 20:46

    I have done this in following manner and worked for me.

    setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(resources.getString(R.string.app_name));
        setSupportActionBar(toolbar);
    

    Use this code after changing local configuration.

    0 讨论(0)
  • 2021-02-18 20:49

    i used another way it seems stupid but it's work

    in each activity try to set the action bar title to your activity title from values file

    in my case i was using sherlockactionbar

    so i added this in oncreate of my productdetails activity

        getSupportActionBar().setTitle(getResources().getString(R.string.title_activity_product_details));
    
    0 讨论(0)
  • 2021-02-18 20:52

    Finally got a solution:


    Get resouce Id of the activity's title from PackageManger, and set the AcionBar's title to the resource id, this time because the Locale is already changed android knows which activity title to pick.

    try{
    
        ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), 
            PackageManager.GET_META_DATA);
    
        ActionBar actionBar = getActionBar();
    
        if(actionBar != null)
            actionBar.setTitle(activityInfo.labelRes);
    
    }catch (PackageManager.NameNotFoundException ex){
    
        Log.e(this.getClass().getSimpleName(), 
            "Error while getting activity info. " + ex.getMessage(), ex);
    
    }
    
    0 讨论(0)
提交回复
热议问题