Reputation:
I have a requirement when user tap Dutch language, the language of all activities must be in Dutch and if English then it should be in English. I have below code. I am able to change language only for one Activity, not for entire application.
When user select English button next activity is displaying in English. Now when user select Russian, that activity must show in Russian language
public class MainActivity extends Activity implements OnClickListener{
private TextView txt_hello;
private Button btn_en, btn_ru, btn_fr, btn_de;
private Locale myLocale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.txt_hello = (TextView)findViewById(R.id.txt_hello);
this.btn_en = (Button)findViewById(R.id.btn_en);
this.btn_ru = (Button)findViewById(R.id.btn_ru);
this.btn_fr = (Button)findViewById(R.id.btn_fr);
this.btn_de = (Button)findViewById(R.id.btn_de);
this.btn_en.setOnClickListener(this);
this.btn_ru.setOnClickListener(this);
this.btn_fr.setOnClickListener(this);
this.btn_de.setOnClickListener(this);
loadLocale();
}
public void loadLocale()
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
public void saveLocale(String lang)
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
private void updateTexts()
{
txt_hello.setText(R.string.hello_world);
btn_en.setText(R.string.btn_en);
btn_ru.setText(R.string.btn_ru);
btn_fr.setText(R.string.btn_fr);
btn_de.setText(R.string.btn_de);
}
@Override
public void onClick(View v) {
String lang = "en";
switch (v.getId()) {
case R.id.btn_en:
lang = "en";
Intent english = new Intent(MainActivity.this, SamplePage.class);
startActivity(english);
break;
case R.id.btn_ru:
lang = "ru";
Intent russia = new Intent(MainActivity.this, SamplePage_Russia.class);
startActivity(russia);
break;
case R.id.btn_de:
lang = "de";
break;
case R.id.btn_fr:
lang = "fr";
break;
default:
break;
}
changeLang(lang);
}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (myLocale != null){
newConfig.locale = myLocale;
Locale.setDefault(myLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
}
Upvotes: 0
Views: 2365
Reputation: 15535
Instead of changing each text manually or writing separate Activity
for each language, you can to do it globally with more feasible. For how please refer the following links.
Supporting Different Languages
You have to create separate values
folder for each language and mention your string resource there. And change your apps local while user tap the button. Android is very smart to find the appropriate resource (values) folder for current local language. If folder not available then the resources loaded from default resource folder.
Upvotes: 1