Why is the first item in the Spinner not working

1.5k views Asked by At

I wanted the spinner to select from the following 3 countries but im only able to access the 2nd and 3rd option. When i select the first option the spinner doesn't work.

 String[] countries = { "INDIA","AUSTRALIA","ENGLAND"};


 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {

            if (!mSpinnerInitialized) {
                mSpinnerInitialized = true;
                return;
            }
            if(spinner.getSelectedItem().toString().equals("INDIA")){
                LocaleManager.setNewLocale(LoginActivity.this, "en");
                restart();

            }else if(spinner.getSelectedItem().toString().equals("AUSTRALIA")){
                LocaleManager.setNewLocale(LoginActivity.this, "si");
                restart();

            }else if(spinner.getSelectedItem().toString().equals("ENGLAND")){
                LocaleManager.setNewLocale(LoginActivity.this, "ta");
                restart();
            }
            Toast.makeText(LoginActivity.this, spinner.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
        }

Spinner Initialization

ArrayAdapter aa = new ArrayAdapter(this,R.layout.languagespinner,countries);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(aa);

The spinner text named INDIA should be accessed but I cannot do it unfortunately.

EDIT

It might be because of the mSpinnerInitialized but if i remove that , the activity is restarted in an endless loop

1

There are 1 answers

10
Blnpwr On

Change your code to:

private String[] arraySpinner;

in onCreate do this:

this.arraySpinner = new String[]{"INDIA", "AUSTRALIA", "ENGLAND"};

Spinner yourSpinner = findViewById(R.id.yourSpinnerID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);


yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
        String selectedItem = parent.getItemAtPosition(position).toString();

        if(selectedItem.equals("INDIA")){
            // DO YOUR LOGIC
        }
    }
}

To change your language use this code:

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static void onCreate(Context context) {

        String lang;
        if(getLanguage(context).isEmpty()){
            lang = getPersistedData(context, Locale.getDefault().getLanguage());
        }else {
            lang = getLanguage(context);
        }

        setLocale(context, lang);
    }

    public static void onCreate(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static void setLocale(Context context, String language) {
        persist(context, language);
        updateResources(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    private static void updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());


    }
}

And in your Spinner Class use this method to change the language:

LocaleHelper.setLocale(this,"en") //for english;
LocaleHelper.setLocale(this, "de") // for german

And for restart use this method:

public static void restartActivity(Activity activity){
    if (Build.VERSION.SDK_INT >= 11) {
        activity.recreate();
    } else {
        activity.finish();
        activity.startActivity(activity.getIntent());
    }
}