Spinner: Get State or Get Notified When Opens

Spinner: get state or get notified when opens

Another option to watch for those events is to extend the Spinner class and use one of its methods(performClick() which will trigger its dialog/popup) followed by monitoring the focus of the window holding this custom Spinner. This should provide you with the wanted closed event for all the possible finishing possibilities(for either the dialog or dropdown mode).

The custom Spinner class:

public class CustomSpinner extends Spinner {

/**
* An interface which a client of this Spinner could use to receive
* open/closed events for this Spinner.
*/
public interface OnSpinnerEventsListener {

/**
* Callback triggered when the spinner was opened.
*/
void onSpinnerOpened(Spinner spinner);

/**
* Callback triggered when the spinner was closed.
*/
void onSpinnerClosed(Spinner spinner);

}

private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;

// implement the Spinner constructors that you need

@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for when the container holding this Spinner may lose focus
mOpenInitiated = true;
if (mListener != null) {
mListener.onSpinnerOpened(this);
}
return super.performClick();
}

@Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasBeenOpened() && hasFocus) {
performClosedEvent();
}
}

/**
* Register the listener which will listen for events.
*/
public void setSpinnerEventsListener(
OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}

/**
* Propagate the closed Spinner event to the listener from outside if needed.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onSpinnerClosed(this);
}
}

/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}

}

How to detect if the Spinner is click open on Android

You can set Touch-listener on Spinner to detect click.

 findViewById(R.id.spinner).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(MainActivity.this,"CallAPI",Toast.LENGTH_SHORT).show();
return false;
}
});

And return False from onTouch to behave normally.I hope It will work for you.

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.



Related Topics



Leave a reply



Submit