Change Language In Android Application Using String File
Solution 1:
Try this example please. Maybe it will help you. Here i used a spinner for selecting language.
In your actvity
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
publicclassAndroidLocalizeextendsActivity {
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(newOnItemSelectedListener() {
publicvoidonItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Tamil", Toast.LENGTH_SHORT)
.show();
setLocale("ta");
} elseif (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected Hindi", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
} elseif (pos == 3) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
}
}
publicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
publicvoidsetLocale(String lang) {
myLocale = newLocale(lang);
Resourcesres= getResources();
DisplayMetricsdm= res.getDisplayMetrics();
Configurationconf= res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intentrefresh=newIntent(this, AndroidLocalize.class);
startActivity(refresh);
}
}
in your XML
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/greet"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/greet"android:textSize="25sp"android:gravity="center"android:paddingTop="25sp" /><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/langselection"android:textAppearance="?android:attr/textAppearanceMedium"android:gravity="center"android:paddingTop="25sp"/><Spinnerandroid:id="@+id/spinner1"android:layout_width="match_parent"android:layout_height="wrap_content"android:entries="@array/languages"android:gravity="center"android:paddingTop="25sp" /></LinearLayout>
and create folders in your res like
then add strings.xml for your language like
<resources><stringname="app_name">Androidlocalization</string><stringname="hello_world">Hello world!</string><stringname="title_activity_android_localize">AndroidLocalize</string><stringname="greet">बधाई सचिन !!</string><stringname="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string><stringname="chooselang">Choose the language</string><string-arrayname="languages"><item>Select language</item><item>தமிழ்</item><item>हिंदी</item><item>English</item></string-array></resources>
please update your manifest also , i hope that will resolve your problem..
update like this.
<applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".AndroidLocalize"android:label="@string/title_activity_android_localize"android:configChanges="locale|orientation|keyboardHidden"android:noHistory="true"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
Solution 2:
This is a method i wrote and is working perfectly well for me for changing the language from app (and JUST FOR A SINGLE APP - not the entire device):
privatevoidsetLanguageForApp(String languageToLoad){
Locale locale;
if(languageToLoad.equals("not-set")){ //use any value for default
locale = Locale.getDefault();
}
else {
locale = newLocale(languageToLoad);
}
Locale.setDefault(locale);
Configuration config = newConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
NOTE: Call this method before setContentView()
in the first activity's onCreate()
everytime when the app is opened.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setLanguageForApp("en"); //hard-coded here - get from whereever you storedsetContentView(R.layout.activity_category_list);
...
...
...
Store the selected locale code in shared preferences and retrieve to pass as parameter.
Method for language selection dialog: (Note: it reloads app after language change to bring the language change in effect)
privatevoidshowLanguageChangePopup() {
CharSequence languages[] = new CharSequence[] {
"English",
"हिंदी (Hindi)",
"Français (French)",
"Italiano (Italian)",
"Deutsch (German)",
"Español (Spanish)",
"日本語 (Japanese)",
"í•œêµì–´ (Korean)",
"Nederlands (Dutch)",
"Português (Portuguese)",
"руÑÑкий (Russian)",
"ä¸æ–‡ (Chinese)",
"العربية (Arabic)"
};
final String codes[] = new String[] {
"en",
"hi",
"fr",
"it",
"de",
"es",
"ja",
"ko",
"nl",
"pt",
"ru",
"zh",
"ar"
};
int currentLangIndex = Prefs.getUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, getBaseContext());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.text_select_language);
builder.setSingleChoiceItems(languages, currentLangIndex, null);
builder.setNegativeButton(R.string.text_translate_cancel, null);
builder.setPositiveButton(R.string.action_change_language, new DialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
int selectedIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
Prefs.setUserPreferenceStringValue(Prefs.Key.LANGUAGE, codes[selectedIndex], getBaseContext());
Prefs.setUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, selectedIndex, getBaseContext());
Intent i = new Intent(CategoryListActivity.this, CategoryListActivity.class);
startActivity(i);
finish();
}
});
builder.show();
}
Solution 3:
Always follow official tutorial
Add your string files here.
if you don't have one, Create values-fil folder in the MyProject/res folder.
Check this if you want to handle Phone system language changes.
Solution 4:
When you are supporting multiple languages , You need to create separate values folder like values-fr for instance and put your stings.xml file inside this folder . Should work. Hope this helps!
Solution 5:
<resources><stringname="app_name">Androidlocalization</string><stringname="hello_world">Hello world!</string><stringname="title_activity_android_localize">AndroidLocalize</string><stringname="greet">बधाई सचिन !!</string><stringname="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string><stringname="chooselang">Choose the language</string><string-arrayname="languages"><item>Select language</item><item>தமிழ்</item><item>हिंदी</item><item>English</item></string-array></resources>
Each code is in same folder for different language add different value folder
For example value folder for hindi goes inside value-hi
Post a Comment for "Change Language In Android Application Using String File"