I have an activity Spinner , which opens in a dialog box using android:spinnerMode="dialog" .

 <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner" android:layout_below="@+id/linear" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" style="@style/spinner_style" android:spinnerMode="dialog"/> 

When I click, with the "dialog" open, to the side, on the activity, the space is outside the dialog box, the dialog closes. Is it possible to call a method like OnNothingSelected , which is in the Spinner setOnItemSelectedListener , when closing this dialog.

Is it possible and how to call the method at all, when exiting the dialog box in the above way?

    2 answers 2

    Alas, in Spinner'а these moments cannot be traced. Alternatively, you can use Button + AlertDialog instead of AlertDialog .

    For example:

     AlertDialog.Builder b = new Builder(this); b.setTitle("Example"); ListAdapter adapter = ....; b.setAdapter(adapter, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { } }); b.show(); 

    You can listen to the moment of cancellation:

     b.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { } }) 

    And closing:

     b.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } }); 
    • Why Button ? The button is a TextView with a background. The usual TextView with the style from Spinner enough. When selecting a value from the list, the position in this TextView will be the same as the Spinner in DialogMode mode - pavlofff
    • @pavlofff agree, it doesn't matter :) - zTrap
    • @pavlofff, can you please show how to implement it with TextView? - lcnw
    • @zTrap, could you help me a little, in terms of code and its structure, please? My application dynamically creates a textview list. With each one, I use the clicker - OnClick , with which the request is OnClick to the database, and I get a list of values. How, when clicking on a textview and getting the result of a query on the screen, display an AlertDialog with this list, or as pavloff said - a drop-down list in Dialog mode like in Spinner ? I do not understand how to write it correctly - lcnw
    • @lcnw If you have a new problem that you cannot solve on your own, create a new question. This question has nothing to do with the problems you describe. - pavlofff

    Here is an example of an inherited Spinner to which you can pass a listener and listen to the spinner open / close events.

    The code itself:

     publiс 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(); } /** * 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; } public void onWindowFocusChanged (boolean hasFocus) { if (hasBeenOpened() && hasFocus) { performClosedEvent(); } } } 
    • in the markup do not need to change anything? How was the Spinner, so it remains? It changes only in the code, where instead of the Spinner type you need to use CustomSpinner, do I understand correctly? - lcnw
    • @Icnw, in the markup, you must replace Spinner with com.example.package.CustomSpinner (replace with your package). In the code, yes, use CustomSpinner and pass it OnSpinnerEventsListener - EgorD