Working on an Android project at the moment. I am trying to implement a button that will allow me to change the language from english to spanish. This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//As soon as app is started up create an order object.
order = new Order();
order.setCovers(2);
order.setTable(1);
order.addToOrder(new MenuItem("Item 1", 12.99));
Toast.makeText(getApplicationContext(), "order size: " + order.getItems().size(), Toast.LENGTH_SHORT).show();
//reference to button and add listeners
orderBtn = (Button)findViewById(R.id.orderBtn);
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Create a new intent and start up the sections activity...
i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", order);
startActivity(i);
}
});
spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
});
}
The button does not change the language. It leaves it as english. However if I run my project with this code at the top of the activity it loads the text in Spanish.
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
What am I doing wrong? Any advice is much appreciate. What I hope to achieve from the above code is that app launches in english getting the string values from the values-en folder. Then when I click the button it loads the string values from the values-es folder.
CHANGE LISTENER TO THIS:
spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.activity_main);
}
});
still not working...
The best thing to do is to reload the activity after setting the default locale. So modify your code to this:
spanishBtn = (Button)findViewById(R.id.spanishBtn);
spanishBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Intent i = new Intent(v.getContext(), YourActivityName.class);
startActivity(i);
}
});
This will just reload the activity with the default language set to Spanish!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.