Change the language of the navigation item

In my Android app, I want to change a fragment of a language form. My application contains MainActivity.java. This action contains a navigation box and a fragment. When I change my language from a fragment, the language changes every time, but not in the navigation box. I am using code

private String setLocal() { Locale locale; Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); prefs = getActivity().getSharedPreferences("Language", 0); String restoredText = prefs.getString("language", ""); if (restoredText.equalsIgnoreCase("fr_FR")) locale = new Locale("fr"); else locale = new Locale("en"); Locale.setDefault(locale); conf = new Configuration(); res.updateConfiguration(conf, dm); return restoredText; } 

When I choose English, I get the following result in English

after changing the language I get this

enter image description here here is a string file from the value-fr directory

  <string-array name="nav_drawer_titles"> <item>Accueil</item> <item>Profil</item> <item>courses</item> <item>Tarification</item> <item>Configuration</item> <item>Envoyez à un ami</item> <item>Facture</item> <item>Position du Chaufeur</item> <item>Aide</item> <item>Déconnexion</item> </string-array> 

and here from the values ​​folder

  <!-- Nav Drawer Menu Items --> <string-array name="nav_drawer_titles"> <item>Home</item> <item>Profile</item> <item>Ride Log</item> <item>Invoice</item> <item>Track Chauffeur</item> <item>Language</item> <item>Refer To Friend</item> <item>Help</item> <item>Sign Out</item> </string-array> 

and my navigationdrawerfragnet is

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { title = getResources().getStringArray(R.array.nav_drawer_titles); images = getResources().getStringArray(R.array.nav_drawer_icons); mDrawerListView = (ListView) inflater.inflate( R.layout.fragment_navigation_drawer, container, false); mDrawerListView .setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } }); selectedposition = new int[] { mCurrentSelectedPosition }; drawerAdapter = new CustomDrawerAdapter(getActivity(), title, images, selectedposition); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); mDrawerListView.setAdapter(drawerAdapter); return mDrawerListView; } 

Im MainActivity.java

 @Override public boolean onPrepareOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onPrepareOptionsMenu(menu); invalidateOptionsMenu(); setLocal(); return super.onPrepareOptionsMenu(menu); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); setLocal(); invalidateOptionsMenu(); } 
+5
source share
2 answers

You must set your language in your activity box before setContentView(int resId) .

Also in the onResume() method you can reassign your activity using API_LEVEL >= 11 call super.recreate() and below 11 you can set onCreate(null) .

+4
source

check this

 title = getResources().getStringArray(R.array.nav_drawer_titles); images = getResources().getStringArray(R.array.nav_drawer_icons); mDrawerListView .setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } }); selectedposition = new int[] { mCurrentSelectedPosition }; drawerAdapter = new CustomDrawerAdapter(getActivity(), title, images, selectedposition); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); mDrawerListView.setAdapter(drawerAdapter); 

you need to push the line there on your setLocal method to invalidate the list of boxes

0
source

Source: https://habr.com/ru/post/1237904/


All Articles