This is almost impossible.
Spinner Class hierarchy
Spinner -> AbsSpinner -> AdapterView<SpinnerAdapter> -> ViewGroup -> View
From Spinner.Java
/**
* <p>A spinner does not support item click events. Calling this method
* will raise an exception.</p>
* <p>Instead use {@link AdapterView#setOnItemSelectedListener}.
*
* @param l this listener will be ignored
*/
We have no access to the click event.
OnClickListener uses public interface DialogInterface.
Best I can suggest is use a popup menu instead of a Spinner if this "feature" bugs you.
@Zvi @Erfan Mowlaei does not seem to post any code. Here's my idea of his idea:
ViewTreeObserver vto1 = spinnerOne.getViewTreeObserver();
vto1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
if( !spinnerOne.getViewTreeObserver().isAlive() )
{
Log.e(TAG,"OnGlobalLayoutListener: ***NOT alive***");
// only need to calculate once, so remove listener
// viewToMeasure.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}else
{
Log.e(TAG,"OnGlobalLayoutListener: ***alive***");
/*when you post a message to a View,
the messages will only be delivered after
the View has been fully initialized (including being measured)*/
spinnerOne .post(new Runnable()
{
@Override
public void run()
{
Log.e(TAG,"OnGlobalLayoutListener: ***run test***");
// safe to get height and width here
spinnerTwo.setSelected(false);
spinnerThree.setSelected(false);
spinnerOne.performClick();
// LayerDrawable ld = (LayerDrawable)spinnerOne.getBackground();//cast problem
// ld.setLayerInset(1, 0, spinnerOne.getHeight() / 2, 0, 0);
}
});
}
}
});
Note:
As I said none of these methods can work, because the events are generated and consumed in the runtime library. They are NEVER exposed.
Other things that do not work:
spinnerOne.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus)
{
spinnerOne.performClick();
Log.v(TAG, "onFocusChange spinnerOne GOT the focus");
Toast.makeText(getApplicationContext(), "spinnerOne got the focus", Toast.LENGTH_SHORT).show();
}else
{
Log.v(TAG, "onFocusChange spinnerOne LOST the focus");
Toast.makeText(getApplicationContext(), "spinnerOne lost the focus", Toast.LENGTH_SHORT).show();
}
}
});
A new hope Episode IV ?
Your problem does raise an entry in logcat every time your perform the "Zvi maneuver":
08-14 01:19:55.575: W/InputEventReceiver(8676):
Attempted to finish an input event but
the input event receiver has already been disposed.
I don't hold much hope for this solving the problem.