jramirez
jramirez

Reputation: 484

ClickListener on a Spinner error

I want to get if a Spinner has been clicked, I'm not interested in what item is selected, only if the user has clicked on the Spinner. When I try it, a compiler error is thrown that shows "Don't call setOnClickListener on a AdapterView...". I know that I've to use an OnItemClickListener, but in this case I want only to catch the click, not the information. I use the same listener in several views for the same process.

Upvotes: 1

Views: 300

Answers (3)

user2413181
user2413181

Reputation:

Your Activity must extend OnItemSelectedListener.
Then, you acitivty must implement 2 functions

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {


}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

Don't forget to add listener on your spinner like this

yourspinner.setOnItemSelectedListener(this);

Upvotes: 0

Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 4522

You can use following code, it may be help you..

Instead of setting the spinner's OnClickListener,try setting OnTouchListener and OnKeyListener.

spinner.setOnTouchListener(spinnerOnTouch);
spinner.setOnKeyListener(spinnerOnKey);
and the listeners:

private View.OnTouchListener spinnerOnTouch = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            //Your code
        }
        return false;
    }
};
private static View.OnKeyListener spinnerOnKey = new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            //your code
            return true;
        } else {
            return false;
        }
    }
};

Upvotes: 1

asenovm
asenovm

Reputation: 6517

You have the following options:

1) Create a class MySpinner that extends Spinner and override its onTouchEvent method. There you can catch if the user has clicked the Spinner.

2) Actually set OnItemClickListener. Basically this listener will be fired any time the Spinner is clicked as it is fired each time an item is clicked and the Spinner itself is nothing but all the items.

Upvotes: 0

Related Questions