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();
}

}

3
  • SavePreferences("", valgtSpraak); - Why are you trying to save preferences with an empty string "" as the key?
    – Squonk
    Apr 30, 2012 at 0:45
  • My bad ofc, just testing. it did ofc have a string when it failed Apr 30, 2012 at 0:49
  • (re)setting content view (creating anew buttons) without setting buttons click listeners in change language function.
    – Deucalion
    Apr 30, 2012 at 7:02

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.