Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an event in Android Spinner when the current selected item is selected again?

Tags:

android

I have written an setOnItemSelectedListener for spinner to respond when the spinner item is changed. My requirement is when I clicks again the currently selected item, a toast should display. How to get this event? When the currently selected item is clicked again, spinner is not responding. `

    StorageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){          @Override         public void onItemSelected(AdapterView adapter, View v, int i, long lng) {                           Toast.makeText(getApplicationContext(), (CharSequence) StorageSpinner.getSelectedItem(), Toast.LENGTH_SHORT).show();          }          @Override         public void onNothingSelected(AdapterView arg0) {             Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();          }     });   
like image 998
indira Avatar asked Mar 17 '11 05:03

indira


People also ask

Which function is used to get the selected item from the spinner?

When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event. The AdapterView. OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. Spinner spinner = (Spinner) findViewById(R.

How can I tell if spinner is clicked or not in android?

Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.

How do I get the spinner selected value in Kotlin?

This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

I spent a good few hours trying to get something to solve this problem. I ended up with the following. I'm not certain if it works in all cases, but it seems to work for me. It's just an extension of the Spinner class which checks the selection and calls the listener if the selection is set to the same value.

import android.content.Context; import android.util.AttributeSet; import android.widget.Spinner;   /** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */ public class NDSpinner extends Spinner {      public NDSpinner(Context context)     { super(context); }      public NDSpinner(Context context, AttributeSet attrs)     { super(context, attrs); }      public NDSpinner(Context context, AttributeSet attrs, int defStyle)     { super(context, attrs, defStyle); }      @Override      public void setSelection(int position, boolean animate) {         boolean sameSelected = position == getSelectedItemPosition();         super.setSelection(position, animate);         if (sameSelected) {             // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now             getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());         }     }       @Override     public void setSelection(int position) {         boolean sameSelected = position == getSelectedItemPosition();         super.setSelection(position);         if (sameSelected) {             // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now             getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());         }     }  } 
like image 193
melquiades Avatar answered Sep 28 '22 05:09

melquiades


try this

public class MySpinner extends Spinner{  OnItemSelectedListener listener;      public MySpinner(Context context, AttributeSet attrs)     {         super(context, attrs);     }      @Override     public void setSelection(int position)     {         super.setSelection(position);          if (position == getSelectedItemPosition())         {             listener.onItemSelected(null, null, position, 0);         }            }      public void setOnItemSelectedListener(OnItemSelectedListener listener)     {         this.listener = listener;     } } 
like image 31
benoffi7 Avatar answered Sep 28 '22 04:09

benoffi7