Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:52:33+00:00 2026-05-28T22:52:33+00:00

My Android app has two Spinners (dropdown menus) working fine. However, now I’ve added

  • 0

My Android app has two Spinners (dropdown menus) working fine.

However, now I’ve added an OptionsMenu with:

  1. One submenu with options to open an AlertDialog textbox.
  2. One submenu with options to change language (locale).

If I click one option in the first submenu, the contents in both of my spinners get deleted, and then the textbox gets opened. When I click myself out of the textbox, the spinners are still cleared.

Similarly, if I click myself into changing language, both of the spinners get cleared, while the rest of the program gets updated to the new language. By restarting the app, all the contents are back.

If it matters, the spinners get their contents from an ArrayAdapter.

So I wonder, how can I update the spinners when they get cleared? I’ve tried “repopulating” the spinners with code in onCreate, onResume or onConfigurationChanged, but it didn’t make any change.

I can find more code if you need, but at least here’s some of it:

Spinner spinner1;
Spinner spinner2;
ArrayAdapter<CharSequence> adapter1;
ArrayAdapter<CharSequence> adapter2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    adapter1 = ArrayAdapter.createFromResource(
            this, R.array.some_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setSelection(1);

    // Same procedure for spinner2...
    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Resources res = getResources();
    SubMenu faqMenu = menu.addSubMenu(0, 100, 1, res.getString(R.string.FAQ)).setIcon(android.R.drawable.ic_menu_rotate);
        faqMenu.add(1, 101, 0, R.string.someText);
    SubMenu langMenu = menu.addSubMenu(0, 200, 2, res.getString(R.string.language)).setIcon(android.R.drawable.ic_menu_rotate);
        langMenu.add(1, 201, 0, "Language1");
        langMenu.add(1, 202, 0, "Language2");
        langMenu.add(1, 203, 0, "Language3");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){

    case 101:
        AlertDialog.Builder textbox = new AlertDialog.Builder(main.this);
        Resources res = getResources();
        textbox.setMessage(res.getString(R.string.someText));
        textbox.show();
        this.setContentView(R.layout.main);
        try{
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);
            spinner1.setSelection(1);
            spinner2.setSelection(2);
        } catch (Exception e){
            e.printStackTrace();
        }
        break;

    case 201:

        Locale locale = new Locale("en"); 
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        Toast.makeText(this, "Here is text in Language1! But unfortunately you need to restart the app before the spinners start working again...sorry!", Toast.LENGTH_LONG).show();
        this.setContentView(R.layout.main);
        try{
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);
            spinner1.setSelection(1);
            spinner2.setSelection(2);
        } catch (Exception e){
            e.printStackTrace();
        }
        break;

    // Similar code for options 202, 203

    }
    return super.onOptionsItemSelected(item);
}

final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        // Perform action on click
        public void onClick(View v) {
            // Make text box
            AlertDialog.Builder textbox = new AlertDialog.Builder(MyActivity.this);
            Resources res = getResources();

            // Get the chosen values from the spinners
            String spinner1choice = spinner1.getSelectedItem().toString();
            String spinner2choice = spinner2.getSelectedItem().toString();

            // Some code for showing the selected items.
     }
}

So…anyone got any help on how to update/refresh the spinners?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:52:34+00:00Added an answer on May 28, 2026 at 10:52 pm

    Remove this.setContentView(R.layout.main);

    before

     try{
                spinner1.setAdapter(adapter1);
                spinner2.setAdapter(adapter2);
                spinner1.setSelection(1);
                spinner2.setSelection(2);
            } catch (Exception e){
                e.printStackTrace();
            }
    

    in both of your cases.

    working code below:

    public class TestSpinner extends Activity {
    
    private ArrayAdapter<CharSequence> adapter1;
    private Spinner spinner1;
    private ArrayAdapter<CharSequence> adapter2;
    private Spinner spinner2;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        adapter1 = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        MyOnItemSelectedListener listener = new MyOnItemSelectedListener();
    
        spinner1.setOnItemSelectedListener(listener);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        adapter2 = ArrayAdapter.createFromResource(
                this, R.array.planets_array1, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter1);
        spinner2.setOnItemSelectedListener(listener);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        SubMenu faqMenu = menu.addSubMenu(0, 100, 1, "FAQ").setIcon(android.R.drawable.ic_menu_rotate);
        faqMenu.add(1, 101, 0, "set other planents");
        SubMenu langMenu = menu.addSubMenu(0, 200, 2, "LANGAUGE").setIcon(android.R.drawable.ic_menu_rotate);
        langMenu.add(1, 201, 0, "Language1");
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
    
            case 101:
                AlertDialog.Builder textbox = new AlertDialog.Builder(TestSpinner.this);
                textbox.setMessage("You've set new planets");
                textbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dlg, int i) {
                        dlg.dismiss();
                    }
                });
                textbox.show();
                try {
                    spinner1.setAdapter(adapter2);
                    spinner2.setAdapter(adapter2);
                    spinner1.setSelection(1);
                    spinner2.setSelection(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            case 201:
                Locale locale = new Locale("en");
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                Toast.makeText(this, "you've set old planets back!", Toast.LENGTH_LONG).show();
                try {
                    spinner1.setAdapter(adapter1);
                    spinner2.setAdapter(adapter1);
                    spinner1.setSelection(1);
                    spinner2.setSelection(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                return true;
        }
        return false;
    }
    
    public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent,
                                   View view, int pos, long id) {
            Toast.makeText(parent.getContext(), "The planet is " +
                    parent.getItemAtPosition(pos).toString(), 1).show();
        }
    
        public void onNothingSelected(AdapterView parent) {
        }
    }
    
    private void refresh() {
            finish();
            Intent myIntent = new Intent(TestSpinner.this, TestSpinner.class);
            startActivity(myIntent);
        }
    }
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Android app has two location listeners, one for fine and one for coarse
My android app has a two word app name, and the 2nd word doesn't
I've got some XML resources in my Android app. Each one has a different
I have an app that has two buttons – one for email & one
I'm working on an Android app which has an activity and a widget. This
I've created a custom button in my Android app that has basically two different
If I have understood correctly, an android process has two heaps - one managed
My android app currently has two layouts for its splash screen, portrait and landscape.
I want to make an android application which has two tap in one view
I’m working on an android application that currently has two Activities, A and B,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.