Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to use <code>setFocusableInTouchMode()</code> first. Then you run into a different problem: you have to tap the spinner twice to change it (once to set focus, then again to see the list of options). My solution is to create my own Spinner subclass that causes the focus gain from the first tap to simulate the second:</p> <pre><code>class MySpinnerSubclass extends Spinner { private final OnFocusChangeListener clickOnFocus = new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // We don't want focusing the spinner with the d-pad to expand it in // the future, so remove this listener until the next touch event. setOnFocusChangeListener(null); performClick(); } }; // Add whatever constructor(s) you need. Call // setFocusableInTouchMode(true) in them. @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { // Only register the listener if the spinner does not already have // focus, otherwise tapping it would leave the listener attached. if (!hasFocus()) { setOnFocusChangeListener(clickOnFocus); } } else if (action == MotionEvent.ACTION_CANCEL) { setOnFocusChangeListener(null); } return super.onTouchEvent(event); } } </code></pre> <p>To give proper credit, I got my inspiration from <a href="https://stackoverflow.com/questions/7241698#15015204">Kaptkaos's answer</a> to <a href="https://stackoverflow.com/questions/7241698">this question</a>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload