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.
To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example: <manifest ... > The only required attribute for this element is android:name, which specifies the class name of the activity.
To create the second activity, follow these steps: In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name. Leave all other properties set to their defaults and click Finish.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With