Rodrigo Costa
Rodrigo Costa

Reputation: 39

how to stop a spinner from automatically selecting an option?

i have a spinner in an app im doing, and when it goes to OnItemSelcted it automatically selects the first option, how do i stop that in a simple way?

 spinner2.setOnItemSelectedListener(new OnItemSelectedListener(){


                        @Override
                        public void onItemSelected(AdapterView<?> parent,
                                View view,
                                int pos, long id) {

Upvotes: 2

Views: 4682

Answers (4)

Abdul Hanan Khan
Abdul Hanan Khan

Reputation: 1

Just add an if Statement to check the position of the item. Add a hint on item at position 0, when item other than position 0 is selected then perform your function as shown below:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        if (position != 0)
        {
            String val = spinner.getSelectedItem().toString();
            // Toast.makeText(activity, formid, Toast.LENGTH_SHORT).show();
        }
    }

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

    }
});

Upvotes: 0

Phurg
Phurg

Reputation: 21

This question has been asked many times on SO and elsewhere. For example:

onNavigationItemSelected in ActionBar is being called at startup how can avoid it?

Infinite loop when using onNavigationItemSelected and invalidateOptionsMenu

Activity loop using onNavigationItemSelected on an ActionBar

Several answers and comments suggest ignoring (some) callbacks. This would work for very simple uses, but it ignores the fact that most people asking this question want a menu not the spinner-based tab-like navigation provided by the standard ActionBar in list mode. Some comments suggest using a PopupMenu, but none that I saw provided details. So here's what to do:

  1. Create the View that will be displayed in the ActionBar and on which the user will click to see the menu:

    View view = this.getLayoutInflater().inflate(R.layout.actionbar_view, null);
    
  2. Create a PopupMenu anchored to that View:

    PopupMenu popup = new PopupMenu(this, view);
    popup.getMenuInflater().inflate(R.menu.actionbar_menu, popup.getMenu());
    
  3. Add an OnClickListener to the View that shows the PopupMenu:

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.show();
        }
    });
    
  4. Add an OnMenuItemClickListener to the PopupMenu to receive notification when the user selects from the menu:

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
        }
    });
    
  5. Finally, use the following to configure the ActionBar in your Activity:

    ActionBar ab = this.getActionBar();
    ab.setCustomView(view);
    ab.setDisplayShowCustomEnabled(true);
    ab.setDisplayShowTitleEnabled(false);
    

One comment is that it can be a bit tricky to get the main View styled to match the default ActionBar. Try investigating the following style resources:

@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title
@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title.Inverse
@android:style/TextAppearance.Holo.Widget.ActionBar.Title
@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse

Otherwise you need at least the right color and text size probably medium. The view itself should obviously fill_parent and have center_vertical gravity.

'Nuff said.

Upvotes: 2

M.Mohsin
M.Mohsin

Reputation: 314

You can use a boolean variable to check if it is first time, do not execute the code. Execute your code second time, otherwise it is the default behavior.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007534

how do i stop that in a simple way?

You don't. A Spinner always has a selection, unless the adapter is empty and no selection is possible. Your app needs to always handle a selection, by one means or another.

Upvotes: 0

Related Questions