Sometimes
it is necessary in the application provide an opportunity the user to change
the language of the application, regardless of the locale device. In this article
I will show you how to do it.
Preparing resources
Create the
string resources for four languages: English, Russian, French and German.
Source code
values/strings.xml (English, Default)
<resources>
<string name="app_name">ChangeLocaleExample</string>
<string name="hello_world">Hello World</string>
<string name="btn_en">English</string>
<string name="btn_ru">Russian</string>
<string name="btn_de">German</string>
<string name="btn_fr">French</string>
</resources>
Source code values-ru/strings.xml (Russian)
<resources>
<string name="hello_world">Привет, Мир</string>
<string name="btn_en">Английский</string>
<string name="btn_ru">Русский</string>
<string name="btn_de">Немецкий</string>
<string name="btn_fr">Французский</string>
</resources>
Source code values-fr/strings.xml (French)
<resources>
<string name="hello_world">Bonjour monde</string>
<string name="btn_en">Anglais</string>
<string name="btn_ru">Russie</string>
<string name="btn_de">Allemand</string>
<string name="btn_fr">Français</string>
</resources>
Source code values-de/strings.xml (German)
<resources>
<string name="hello_world">Hallo Welt</string>
<string name="btn_en">Englisch</string>
<string name="btn_ru">Russisch</string>
<string name="btn_de">Deutsch</string>
<string name="btn_fr">Französisch</string>
</resources>
Layout application will consist of four buttons that change the language of the application and the text box to display the greeting line:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_en" />
<Button
android:id="@+id/btn_ru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_ru" />
<Button
android:id="@+id/btn_de"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_de"
/>
<Button
android:id="@+id/btn_fr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_fr" />
</LinearLayout>
</LinearLayout>
Developing an application
Add
attributes to our Activity.
- UI elements:
private TextView txt_hello;
private Button btn_en, btn_ru, btn_fr, btn_de;
- Current local application:
private Locale myLocale;
Develop
necessary methods to change the language in the application.
- Changing the language in the application:
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();
}
- Save the current locale:
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();
}
- Loading a saved locale:
public void loadLocale()
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
- Updating the UI elements of the current screen (you need to update only the screen in which a change of locale):
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);
}
Add events
to the buttons. For this implement OnClickListener interface for our Activity
(implements OnClickListener). And implement the method onClick().
public void onClick(View v) {
String lang = "en";
switch (v.getId()) {
case R.id.btn_en:
lang = "en";
break;
case R.id.btn_ru:
lang = "ru";
break;
case R.id.btn_de:
lang = "de";
break;
case R.id.btn_fr:
lang = "fr";
break;
default:
break;
}
changeLang(lang);
}
Finally,
consistent with the work with the change of language in the method onCreate ()
of our Activity:
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();
}
Links
- The source codes of this project can be downloaded here: zip


excellent ! It is very helpful post for developers.
ReplyDeleteandroid application development