0

I am trying to implement language change using an spinner located in the toolbar. Right now I have the following code for the spinner:

initList();
Spinner spinnerCountries = findViewById(R.id.spinner_limba);
mAdapter = new SpinnerAdapterTara(Harta.this, mCountryList);
    spinnerCountries.setAdapter(mAdapter);
    spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
        String clickedCountryName = clickedItem.getCountryName();
        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

The initList(); creates the list of languages. The problem right here it's about the onItemSelected , because if I choose fr or en, the setLocale("fr")/setLocale("en"); recreate(); will run continously, freezing my app. I need a solution to implement the onItemSelected only once, like a onChangeListener or something. The app should change it's language when the spinner value is changed.

EDIT: I added the setLocale():

private void setLocale(String limba){
    Locale locale = new Locale(limba);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale= locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences.Editor editor = getSharedPreferences("Setare limba", MODE_PRIVATE).edit();
    editor.putString("Limba mea", limba);
    editor.apply();
}

EDIT: added initList()

private void initList() {
    mCountryList = new ArrayList<>();
    mCountryList.add(new TaraITEM("RO", R.drawable.steag_ro));
    mCountryList.add(new TaraITEM("EN", R.drawable.steag_en));
    mCountryList.add(new TaraITEM("FR", R.drawable.steag_fr));
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
Costin
  • 194
  • 1
  • 2
  • 16

1 Answers1

2

Use static variable to hold the last selection and check that before recreating the activity which give you flavor like onChangeListener

//Place inside Harta like global variable
static String lastCountryName = "";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
    String clickedCountryName = clickedItem.getCountryName();

    // compare here before recreating
    if(!clickedCountryName.equals(lastCountryName)) {
        lastCountryName = clickedCountryName;

        if (clickedCountryName.equals("FR")){
            setLocale("fr");
            recreate();
        } else if (clickedCountryName.equals("EN")){
            setLocale("en");
            recreate();
        }
    }
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46