Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
Dropdown android - call setOnItemSelectedListener only when user change its value

I have a dropdown that I set some values on it and show, as default, the last value user saved from shared pref. It is working well. The problem is when I start the activity, to put this saved user value on the select, as default, it is calling setOnItemSelectedListener and showing a toast to the user. I do not want to run the block code inside setOnItemSelectedListener this time, just when the user changes the dropdown value. Any idea on how to solve it?

String defaultValue = pref02.getString("fonte", "Normal");
String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
int spinnerPosition = adapter.getPosition(defaultValue);
dropdown.setSelection(spinnerPosition); //select the atual saved value.
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref2.edit();

        String drop = dropdown.getSelectedItem().toString();
        editor.putString("fonte", drop);
        editor.apply();
        Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Solution: Using the below code (you can check comment lines to see the guide)

String defaultValue = pref02.getString("fonte", "Normal");
String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
int spinnerPosition = adapter.getPosition(defaultValue);
dropdown.setSelection(spinnerPosition); //select the atual saved value.

// Add this block of code
dropdown.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        // Remove listener to make sure it doesn't called later.
        dropdown.removeOnLayoutChangeListener(this);

        // Set your listener here
        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref2.edit();

                String drop = dropdown.getSelectedItem().toString();
                editor.putString("fonte", drop);
                editor.apply();
                Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }
        });
    }
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!