If you have a handle to a Spinner object in an Android Activity, can you programmatically pop open the spinner options - thereby forcing the user to choose an option even though they did not click on the Spinner
themselves?
-
what do u mean by spinner object, can u share mock screenshots of what u want to do – the100rabh Apr 21 '10 at 04:30
-
`performClick()` may not be enough, if you get the `Unable to add window` error, see the what to do [here](https://stackoverflow.com/a/16032355/8958408). – Protean Apr 21 '18 at 13:40
6 Answers
To open the Spinner you just need to call it's performClick() method.
Keep in mind that you may only call this method from the UI thread. If you need to open the Spinner from a separate thread you should create a Handler in the UI thread and then, from your second thread, send a runnable object that calls performClick() to the Handler.
package com.example.SpinnerDemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.os.Handler;
public class SpinnerDemo extends Activity {
private Handler h;
private Spinner s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
h = new Handler();
s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
// Open the Spinner...
s.performClick();
// Spawn a thread that triggers the Spinner to open after 5 seconds...
new Thread(new Runnable() {
public void run() {
// DO NOT ATTEMPT TO DIRECTLY UPDATE THE UI HERE, IT WON'T WORK!
// YOU MUST POST THE WORK TO THE UI THREAD'S HANDLER
h.postDelayed(new Runnable() {
public void run() {
// Open the Spinner...
s.performClick();
}
}, 5000);
}
}).start();
}
}
The resources used by this example can be found here.

- 11,166
- 4
- 52
- 41
-
Thanks. performClick() was what I was looking for. I should have seen that method on the spinner but missed it due to some confusion on my end. Thanks a lot! – JohnRock Apr 22 '10 at 02:19
-
What if I used runOnUIThread for using perform click from different thread. – Relsell Feb 16 '12 at 10:28
-
Worked before 5.0.2, seems to be broken on 5.0.2 (only for some devices), but working again on 5.1... – Benjamin Piette Oct 14 '15 at 09:20
-
When using a dialog my spinner gets open behind the dialog using performClick(); Any solution for this? – Huzaifa Asif Dec 21 '20 at 07:17
To show the Spinner
items you just need to call it's performClick()
method.
Spinner spDeviceType = (Spinner) findViewById(R.id.spDeviceType);
spDeviceType.performClick();

- 8,057
- 8
- 35
- 54

- 2,198
- 1
- 29
- 29
You don't need to use 2 runnables as shown in the previous example.
This will be enough :
h.postDelayed(new Runnable() {
public void run() {
s.performClick();
}
}, 5000);

- 50,140
- 28
- 121
- 140

- 179
- 1
- 2
-
4I think it's just a demo to show the handler working from a separate thread. – Mark Renouf Feb 24 '12 at 19:21
Simply use this
yourspinner.performClick();

- 67,701
- 16
- 123
- 163

- 850
- 9
- 19
@Override
protected void onResume() {
super.onResume();
_spinner_operations.performClick();
}
you need the call in onResume, in onCreate this not work.
You can call performClick()
after the UI thread is done with its current operation(s). If you don't use post {}
, you may not see the Spinner open.
findViewById<Spinner>(R.id.spinner).post {
performClick()
}

- 19,087
- 4
- 72
- 54