I'm trying to implement multiple language support in my applicaiton.
My app contains two activities namely MainActivity.class and NextActivity.class.
When the user selects any language option from the MainActivity.class, i will save the selected language in sharedpreference
and get the sharedpreference
language in next page to change the language type. But unable to change the language type.
Can anyone please help me to sort out this issue? Thanks for your precious time!..
strings.xml (values-fr)
<resources>
<string name="app_name">myAPP</string>
<string name="action_settings">Settings</string>
<string name="hello">Bonjour !..</string>
</resources>
strings.xml (values-hi)
<resources>
<string name="app_name">myAPP</string>
<string name="action_settings">Settings</string>
<string name="hello">नमस्ते !..</string>
</resources>
MainActivity.java
public class MainActivity extends AppCompatActivity {
CheckBox chk_english, chk_french, chk_hindi;
Button btn_next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
chk_french = (CheckBox) findViewById(R.id.chk_french);
chk_hindi = (CheckBox) findViewById(R.id.chk_hindi);
btn_next = (Button) findViewById(R.id.btn_next);
chk_french.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
chk_hindi.setChecked(false);
}
saveLanguage("fr");
}
});
chk_hindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton.isChecked()) {
chk_french.setChecked(false);
}
saveLanguage("hi");
}
});
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, NextActivity.class));
}
});
}
public void saveLanguage(String type) {
SharedPreferences.Editor editor = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE).edit();
editor.putString("myLanguage", type);
editor.apply();
editor.commit();
}}
NextActivity.java
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_layout);
}
@Override
protected void onResume() {
super.onResume();
loadLanguage();
}
public void loadLanguage() {
SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
String myLang = prefs.getString("myLanguage", "");
if (myLang.equals("fr")) {
setLanguage("fr");
} else if (myLang.equals("hi")) {
setLanguage("hi");
} else if (myLang.equals("en")) {
setLanguage("en");
}
}
void setLanguage(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.next_layout);
}}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.org.myapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<CheckBox
android:id="@+id/chk_french"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="French"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"/>
<CheckBox
android:id="@+id/chk_hindi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hindi"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"
/>
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Next"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
next_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="test.org.myapp.NextActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
First check Are you getting the sharedprefrence value by putting log, if yes then change your code to my below code.
Log.e("LanguageType", prefs.getString("myLanguage", ""));
Change your code to my below code,Sure it will work for you
public void loadLanguage() {
SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
// String myLang = prefs.getString("myLanguage", "");
if (prefs.getString("myLanguage", "").equals("fr")) {
setLanguage("fr");
} else if (prefs.getString("myLanguage", "").equals("hi")) {
setLanguage("hi");
} else if (prefs.getString("myLanguage", "").equals("en")) {
setLanguage("en");
}
}