monchyrcg
monchyrcg

Reputation: 382

How to change the language of the options menu?

I want to change the language of my options menu. This code works in all my activity excepts menu.

public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.ingles:
            mLocale = new Locale("en");
            Locale.setDefault(mLocale); 
            config = getBaseContext().getResources().getConfiguration(); 
            if (!config.locale.equals(mLocale)) { 
                config.locale = mLocale; 
                getBaseContext().getResources().updateConfiguration(config, null); 
            }
            setContentView(R.layout.main);
            break;

        case R.id.euskera:
            mLocale = new Locale("eu");
            Locale.setDefault(mLocale); 
            config = getBaseContext().getResources().getConfiguration(); 
            if (!config.locale.equals(mLocale)) { 
                config.locale = mLocale; 
                getBaseContext().getResources().updateConfiguration(config, null); 
            }
            setContentView(R.layout.main);
            break;
    }
    return super.onOptionsItemSelected(item);
}

why? I tried with onPrepareOptionsMenu but it creates additional menus

public boolean onPrepareOptionsMenu(Menu menu) {

    if(config.equals(this.getBaseContext().getResources().getConfiguration())){
        MenuInflater inflater = this.getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    return false;

}

Upvotes: 4

Views: 3962

Answers (1)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

Better to put the menu title string in Values string.xml and set in menu.xml from there

like

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"

          android:title="@string/new_game"
          />

</menu>

and if you want to change it at run time refer this

Change language programmatically in Android

Upvotes: 1

Related Questions