Textview Not Changing When Changing Locale Of App
I am trying to change the locale of application when selecting options from the onOptionsItemSelected. When I try to do so, the language gets successfully changed in the same menu.
Solution 1:
Try this use finish();
after restarting your activity
Intent refresh = newIntent(this, MainActivity.class);
startActivity(refresh);
finish();
Solution 2:
There are two different approaches:
1. Without Activity restart:
Set
android:configChanges="locale"
in your<activity>
in manifestChange your setLocale as follows:
privatevoidsetLocale(String lang) { LocalemyLocale=newLocale(lang); Resourcesres= getResources(); DisplayMetricsdm= res.getDisplayMetrics(); Configurationconf= res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics()); invalidateOptionsMenu(); onConfigurationChanged(conf);//Add this line }
Override
onConfigurationChanged()
:@OverridepublicvoidonConfigurationChanged(final Configuration newConfig) { super.onConfigurationChanged(newConfig); textView.setText(<your-text>); //Any other UI text to change }
This will ensure your locale is changed correctly.
2. Recreate the activity:
Instead of
Intent refresh = newIntent(this, MainActivity.class);
startActivity(refresh);
finish();
CALL
recreate();
This will cause this Activity to be recreated with a new instance.
Post a Comment for "Textview Not Changing When Changing Locale Of App"