Example- How to Support Different Languages in Android ?
Exaplain-Create Loacale Directories (values folder) and
string file . e.g we are used Indonesian
language so create values-in folder show below Image.
......................................................................................................................................................................................................................................................................................................................................
Activity_main.xml.
...................................................................................................................................................................
xml
version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rd_group"
android:layout_marginLeft="140dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
android:layout_gravity="right|top"
android:id="@+id/enlish"
android:onClick="onRadioButtonClicked"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indonesian"
android:id="@+id/indonesia"
android:onClick="onRadioButtonClicked"
android:layout_marginLeft="12dp"
android:checked="false"
/>
</RadioGroup>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/rd_group"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:id="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_marginTop="33dp"
android:id="@+id/editText2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_marginTop="88dp"
android:id="@+id/button"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="42dp"
android:id="@+id/textView" />
<TextView
android:text="Android Beginner point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0f8c0f"
android:textStyle="bold"
android:textSize="18dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView2" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rd_group"
android:layout_marginLeft="140dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English"
android:layout_gravity="right|top"
android:id="@+id/enlish"
android:onClick="onRadioButtonClicked"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Indonesian"
android:id="@+id/indonesia"
android:onClick="onRadioButtonClicked"
android:layout_marginLeft="12dp"
android:checked="false"
/>
</RadioGroup>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/rd_group"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:id="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_marginTop="33dp"
android:id="@+id/editText2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_marginTop="88dp"
android:id="@+id/button"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="42dp"
android:id="@+id/textView" />
<TextView
android:text="Android Beginner point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0f8c0f"
android:textStyle="bold"
android:textSize="18dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView2" />
</RelativeLayout>
...................................................................................................................................................................
LocaleHelper.java
...................................................................................................................................................................
public
class LocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static void onCreate(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
setLocale(context, lang);
}
public static void onCreate(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static void setLocale(Context context, String language) {
persist(context, language);
updateResources(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
private static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
}
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static void onCreate(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
setLocale(context, lang);
}
public static void onCreate(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static void setLocale(Context context, String language) {
persist(context, language);
updateResources(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
private static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
}
...................................................................................................................................................................
MainActivity.java
...................................................................................................................................................................
public
class MainActivity extends
AppCompatActivity {
Resources resources;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocaleHelper.setLocale(this, "en");
UpdateView();
}
public void UpdateView(){
resources = getResources();
((EditText)findViewById(R.id.editText)).setHint(resources.getString(R.string.UserId));
((EditText)findViewById(R.id.editText2)).setHint(resources.getString(R.string.UserPassword));
((TextView)findViewById(R.id.textView)).setText(resources.getString(R.string.fogetpass));
((Button)findViewById(R.id.button)).setText(resources.getString(R.string.Userlogin));
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.enlish:
if (checked)
LocaleHelper.setLocale(this, "en");
UpdateView();
break;
case R.id.indonesia:
if (checked)
LocaleHelper.setLocale(this, "in");
UpdateView();
break;
}
}
}
Resources resources;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocaleHelper.setLocale(this, "en");
UpdateView();
}
public void UpdateView(){
resources = getResources();
((EditText)findViewById(R.id.editText)).setHint(resources.getString(R.string.UserId));
((EditText)findViewById(R.id.editText2)).setHint(resources.getString(R.string.UserPassword));
((TextView)findViewById(R.id.textView)).setText(resources.getString(R.string.fogetpass));
((Button)findViewById(R.id.button)).setText(resources.getString(R.string.Userlogin));
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.enlish:
if (checked)
LocaleHelper.setLocale(this, "en");
UpdateView();
break;
case R.id.indonesia:
if (checked)
LocaleHelper.setLocale(this, "in");
UpdateView();
break;
}
}
}
...................................................................................................................................................................
0 comments