0

Im using this code to make a hangman application, but if i chose a language first nothing happen if i push the three button (nyttSpillKnapp, omSpilletKnapp,avsluttKnapp) I donno why. If i dont chose a language in the OptionMenu i can play the game.

public class HangmanActivity extends Activity implements OnClickListener {
private static final String TAG = "Hangman";
SharedPreferences spraak;
String valgtSpraak = "uk";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settSpraak("uk");
    setContentView(R.layout.main);
    Button nyttSpillKnapp = (Button) this.findViewById(R.id.nyttSpill);
    nyttSpillKnapp.setOnClickListener(this);

    Button omSpilletKnapp = (Button) this.findViewById(R.id.omSpillet);
    omSpilletKnapp.setOnClickListener(this);
    Button avsluttKnapp = (Button) this.findViewById(R.id.avslutt);
    avsluttKnapp.setOnClickListener(this);
}
public void settSpraak(String spraak) {
    valgtSpraak = spraak;
    Locale locale = new Locale(valgtSpraak);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.nyttSpill:
        SavePreferences("", valgtSpraak);
        startActivity(new Intent(this, test.class));
        break;
    case R.id.omSpillet:
        //SavePreferences("", valgtSpraak);
        startActivity(new Intent(this, omSpillet.class));
        break;
    case R.id.avslutt:
        finish();
        break;
        default:
    }
}
public boolean onCreateOptionsMenu(Menu meny) {
    super.onCreateOptionsMenu(meny);
    meny.add(R.string.norsk);
    meny.add(R.string.engelsk);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item){
    SavePreferences("", valgtSpraak);
    if (item.getTitle().equals(getResources().getString(R.string.norsk))){
        settSpraak("no");
    }
    if(item.getTitle().equals(getResources().getString(R.string.engelsk))){
        settSpraak("uk");
    }
    return true;
}
private void SavePreferences(String key, String value) {
    SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    SharedPreferences.Editor prefsEditor;
    prefsEditor = myPrefs.edit();
    prefsEditor.putString(key, value);
    prefsEditor.commit();
}

}

Joakim Hansen
  • 117
  • 2
  • 9

1 Answers1

0

I'm guessing the settSpraak(...) method is silently failing when trying to set the Locale. Have you checked logcat?

I don't think you have the correct locale strings, for example UK should be en_GB. Try the following for UK and Norway respectively...

Locale locale = new Locale("en", "GB");
Locale locale = new Locale("no", "NO");

Note case-sensitivity for language and country.

Squonk
  • 48,735
  • 19
  • 103
  • 135