Troubles with activity title language

I have two language in my app. values/strings.xml and values-ru/strings.xml When I programmatically change language, all strings translating, but activity title is unchanged. I use in all Activities

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String languageToLoad  = prefs.getString("prefLanguage","en");
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());

When I select language, activity or application is reloaded.

Answers


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));

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));
            }
        });
    }
}

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);

}

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.


Need Your Help

Decrease image size without losing its quality in android

android image bitmap

How to decrease image size without losing its quality in android.