I have encountered a very peculiar problem. I want to change my app's language and to do so I used the code I found here, on stackoverflow:
private void restartActivityInLanguage(String language) {
Locale locale = new Locale(language);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
getActivity().recreate();
}
polishLanguage = view.findViewById(R.id.polish_language);
englishLanguage = view.findViewById(R.id.english_language);
polishLanguage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
restartActivityInLanguage("pl");
}
});
englishLanguage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
restartActivityInLanguage("en");
}
});
This code works perfectly with the main part of my app but there're places where the text doesn't change and stays in the same language version as my whole phone, the places are:
popup menus
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context wrapper = new ContextThemeWrapper(context, R.style.PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, v);
popup.inflate(R.menu.browse_location_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.browse_location_edit:
onProductClick.onClick(locationModel);
break;
case R.id.browse_location_delete:
onProductClick.onDelete(locationModel);
break;
default:
break;
}
return true;
}
});
popup.show();
}
});
<item
android:id="@+id/browse_location_edit"
android:title="@string/browse_location_edit" />
<item
android:id="@+id/browse_location_delete"
android:title="@string/browse_location_delete"/>
-tablayout titles
TabLayout tabLayout = findViewById(R.id.tab_layout);
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) ->
{
switch (position) {
case 0:
tab.setText(getString(R.string.add_product));
break;
case 1:
tab.setText(getString(R.string.add_location));
break;
}
}
).attach();
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/TabLayoutHeight"
android:background="@color/IvoryBackground"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
I am using text from string.xml file that contains strings in two language versions.
I have run out of ideas as to what may cause this problem. There's no error in logcat, the text just doesn't change. Any help and suggestions will be appreciated :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…