Search code examples
javaandroidandroid-resources

Android app language changes everywhere but not in menu and tab layout title


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 :)


Solution

  • In TabLayout the only working solution was:

    getBaseContext().getString(R.string.my_string);
    

    Popup menus were more tricky as they were adapters used in fragments and Zain solutions didn't work. I have the solution there: Android: In ViewPagerAdapter how to get getBaseContext() My code in one of many fragments before was:

    locationsAdapter = new LocationsAdapterDB(getActivity().getApplicationContext(),
    

    I changed it to:

    locationsAdapter = new LocationsAdapterDB(getActivity().getBaseContext(),
    

    And in the adapter class itself I added:

    public class Adapter extends PagerAdapter {
    Context mContext;
    
    public Adapter(Context context) {
        mContext = context;
    }
    
    }