How to Get an Event in Android Spinner When the Current Selected Item Is Selected Again

Spinner already Selected Item Selection event

To call an event on the already selected item in Spinner You can do it in this way:

  1. Create Your own Spinner class by extending AppCompatSpinner:
import android.content.Context
import android.util.AttributeSet
import android.util.Log

class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
context,
attrs
)
{
var listener: OnItemSelectedListener? = null

override fun setSelection(position: Int)
{
super.setSelection(position)
if (position == selectedItemPosition)
{
listener!!.onItemSelected(this, selectedView, position, selectedItemId)
}
}

override fun setOnItemSelectedListener(listener: OnItemSelectedListener?)
{
this.listener = listener
}
}

  1. Use it in Your layout:
<com.yourcompany.kotlintest.MySpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

  1. When creating a layout do this:
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val spinner = findViewById<MySpinner>(R.id.spinner)

spinner.adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_dropdown_item,
arrayListOf("One", "Two", "Three")
)

spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener
{
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
)
{
Log.d("MyTag", "Click item at pos $position")
}

override fun onNothingSelected(parent: AdapterView<*>)
{
Log.d("MyTag", "Nothing selected")
}
}
}
}

Now when You reselect the same item function will be executed

Android Spinner: Get the selected item change event

Some of the previous answers are not correct. They work for other widgets and views, but the documentation for the Spinner widget clearly states:

A spinner does not support item click
events. Calling this method will raise
an exception.

Better use OnItemSelectedListener() instead:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}

});

This works for me.

Note that onItemSelected method is also invoked when the view is being build, so you can consider putting it inside onCreate() method call.

android spinner fire event when same item selection is made

use click listener to fulfill your requirement. as direct click listener on spinner doesn't supported so make a class extend spinner and over ride on click method and in this method do what you want to do.

Spinner's currently selected item is repeatedly selected

Make widget of MySpinner in your xml. Then Replace

public static Spinner mSPI_DRS4DL, mSPI_DRS4D_NXT;

with

public static MySpinner mSPI_DRS4DL, mSPI_DRS4D_NXT;

Android Spinner On Re-Select Item

I achieve reselection callback in onItemSelected by this custom spinner code.
Ref: https://stackoverflow.com/a/11323043/9909365

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());
}
}

}

Changing Spinner list content after item is selected Android

your code is a dead loop. and if the adapter item data changed,
such as add, remove, clear by adapter you can use notifyDataSetChanged
method.

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

adapter.clear()/add(object)/addAll();
adapter.notifyDataSetChanged();

then the view will update but not init.

if the whole data changed, you can also recreate an adapter, and then set data

dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, newStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(dataAdapter)

At the same time, you can also clear and addAll, then notifyDataSetChanged

adapter.clear() //remove all data;
adapter.addAll(source);
adapter.notifyDataSetChanged();

//or remove it, then insert it in the first. that is acturl for your conditon
adapter.remove(objcet) //remove special data;
adapter.insert(object,index);
adapter.notifyDataSetChanged();

Secondly, you can also set a Tag to make when init the onItemSelected not called. the code like this:

//define a tag when first it is 0
int check = 0;
//then in the onItemSelected add a check condition
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(++check> 1) {
//do some things when item selected.
}
}

use notifyDataSetChanged to update data, not every time init spinner. if you only want to remove the init selected effect, use the check tag.

How to make a spinner show previously selected item after refresh?

If you want to set the previous selected item of a spinner you have to store the selected item and then set your selection :
here's an example wish i'am getting my previous selected item then fetching the item's position from he's array adapter

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String language = preferences.getString("language", "");
if(!language.equalsIgnoreCase(""))
{
int spinnerPosition = arrayAdapter.getPosition(language);
spinner.setSelection(spinnerPosition);

}

You can store your data as below :

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

SharedPreferences.Editor editor = preferences.edit();
editor.putString("language",spinner.getSelectedItem().toString(););
editor.apply();



}


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

}
});


Related Topics



Leave a reply



Submit