I have two language in my app. values/strings.xml and values-ru/strings.xml When I programmatically change language, all strings translating, b
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));
}
});
}
}