Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change navigationdrawer item language

In my android app I want to change language form fragment.My app contains MainActivity.java. This activity contains navigation drawer and fragment. When I change my locale from fragment, language is changed every where but not in navigation drawer.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 select English I get following resultin English

after changing language I get this one

enter image description here here is string file from values-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 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();
}
like image 571
Rahul Patel Avatar asked Oct 19 '22 20:10

Rahul Patel


1 Answers

You should set your locale in your drawer activity before setContentView(int resId).

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

like image 127
Piyush Avatar answered Nov 15 '22 05:11

Piyush