Actually i am creating a multilingual(English,Hindi and Punjabi) android application so i saved the user language preference in shared preference and check in onCreate() in every activity and update the strings. But the issue is when i choose the Hindi language, whole activity string changed to Hindi correctly but the bottom Navigation is set to English only and when i choose Punjabi than whole activity string convert perfectly but than bottom navigation set to Hindi and so on in cyclic manner, please check below test case
user Selected Language | Bottom Navigation Language
|
First time: English | English
Second time: Hindi | English
Third time: Punjabi | Hindi
Fourth time: English | Punjabi
Fifth time Hindi | English
and so on...
Code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionManager = new SessionManager(getApplicationContext());
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
userDefaultLanguage(sessionManager.getUserLanguage());
initViews();
}
Locale myLocale;
String currentLanguage = "en";
private void userDefaultLanguage(String localeName) {
myLocale = new Locale(localeName);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
private void initViews() {
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
loadFragment(MainFragment.newInstance());
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
toolbar_title.setText(R.string.home);
if (CheckInternet.isNetwork(getApplicationContext())) {
loadFragment(MainFragment.newInstance());
} else
connectToInternetToast();
return true;
case R.id.navigation_music:
if (CheckInternet.isNetwork(getApplicationContext())) {
toolbar_title.setText(R.string.music);
// toolbar_title.setTypeface(applyFonts());
loadFragment(MainFragment2.newInstance());
} else
connectToInternetToast();
return true;
case R.id.navigation_gyaan:
if (CheckInternet.isNetwork(getApplicationContext())) {
toolbar_title.setText(R.string.gallery);
loadFragment(MainFragment3.newInstance());
} else
connectToInternetToast();
return true;
case R.id.navigation_settings:
if (CheckInternet.isNetwork(getApplicationContext())) {
Intent intent2 = new Intent(getApplicationContext(), AppInformationActivity.class);
startActivity(intent2);
} else
connectToInternetToast();
return true;
}
return false;
}
});
toolbar_title.setText(R.string.home);
if (CheckInternet.isNetwork(getApplicationContext())) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
} else
connectToInternetToast();
}
private void connectToInternetToast() {
Toast.makeText(getApplicationContext(), "Connect to internet", Toast.LENGTH_LONG).show();
}
private void loadFragment(Fragment selectedFragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
}
}
I have found the solution, please check below code:
public static void userDefaultLanguage(Activity context,String localeName) {
Locale locale;
//Log.e("Lan",session.getLanguage());
locale = new Locale(localeName);
Configuration config = new
Configuration(context.getResources().getConfiguration());
Locale.setDefault(locale);
config.setLocale(locale);
context.getBaseContext().getResources().updateConfiguration(config,
context.getBaseContext().getResources().getDisplayMetrics());
}
and call this method as
setLocale(MainActivity.this,sessionManager.getUserLanguage());