How to capture onClick event in Android for a spinner
Asked Answered
D

3

18

I want to capture the onClick event when the user selects a spinner value.

I've tried implementing OnClickListener and using the following code:

@Override
public void onClick(final View view) {
  if (view == countrySpinner) {
    Toast.makeText(this, "Override OK!", 3);
  }
}

And binding with:

countrySpinner.setOnClickListener(this);

This compiles, but I get a RuntimeException advising me to use OnItemClickListener rather than OnClickListener for an AdapterView.

How can I capture that onClick event?

Dyspnea answered 18/4, 2012 at 15:42 Comment(4)
Spinner does not support click events, as the exception plainly tells you. Either use selection events or do not use a Spinner.Tamanaha
@CommonsWare, how then would I go about loading Spinner list items from a Web Service without doing it eagerly? Is it just not possible to do this? (Seems like a large limitation!)Dyspnea
Loading content into a Spinner has nothing to do with click events, any more than generating a Web page has anything to do with somebody clicking a link on the page itself once loaded in a browser. I have no idea what you think "eagerly" is. If you have questions on populating a Spinner, please open a fresh SO question.Tamanaha
As CommonsWare says you don't need to wait until the users clicks your spinner to populate it. You populate it by setting an ArrayAdapter. There are tutorials out there, and one here (although they use local resources to populate the spinner). Just do a webcall to get your spinner items and then create an ArrayAdapter.Sevier
S
23

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;
        }
    }
};
Snatchy answered 18/4, 2012 at 15:52 Comment(10)
Your code will make the Spinner completely unusable, by consuming all touch and key events. If that is the objective, the OP should simply use a Button.Tamanaha
@Tamanaha I hope this will work now... by mistake I return true in onTouchListener. It should be falseSnatchy
I doubt that will help. Chain to the superclass if you want the superclass to have its normal behavior.Tamanaha
I checked it and it is working on my end. Spinner is behaving normallySnatchy
@CommonsWare: this is exactly what I wanted. A Button does not look like a spinner.Urinalysis
@VioletGiraffe: That is why you should not do this. The user will click the Spinner, expecting the typical Spinner behavior. The user will be confused when your Spinner does not behave as expected.Tamanaha
@CommonsWare: I understand you and generally agree with that, but in my a case a spinner makes for a cleaner UI even though it doesn't behave exactly like a normal spinner.Urinalysis
This doesn't work for screenreader users. Any idea how to intercept the talkback onClick event?Stephaniastephanie
How about some credit? Your answer was copied and pasted from here: #3928571Sext
this solution misses to validate if a touch is a click. E.g. touching the spinner and swipe away from it will be handled like a touch, so ANY touch down on the spinner will be handled as a click, you can't cancel it anymore...Weatherman
B
16

Don't treat a spinner like a button, buttons have onClick events. Spinners have onItemSelected events.

You should be capturing the Spinner's onItemSelected event like this:

import android.widget.AdapterView;

Spinner productname_spinner =(Spinner) findViewById(R.id.your_spinner);

productname_spinner.setOnItemSelectedListener(
    new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        Object item = parent.getItemAtPosition(pos);
        System.out.println(item.toString());     //prints the text in spinner item.

    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

In Kotlin, similar to the following:

mySpinner.onItemSelectedListener = object: OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {
        // Do nothing
    }
    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val selection = parent?.getItemAtPosition(position)
        viewModel.updateSelection(selection as String)
    }
}
Buskus answered 3/11, 2014 at 0:19 Comment(0)
L
0

I've tried implementing OnItemSelectedListener to get Selected Spinner Value , using the following code:

String selectAreaNameString;

AreaSpinner.setOnItemSelectedListener(AreaSpinnerSelected = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

            Object item = parent.getItemAtPosition(pos);


            selectAreaNameString = item.toString();
});
Lacerated answered 13/8, 2019 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.