Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to programmatically open a Spinner in Android app?

Tags:

android

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?

like image 767
JohnRock Avatar asked Apr 21 '10 01:04

JohnRock


People also ask

How can we check whether a spinner is open or not in android?

We can user onClickListner for spinner. Since spinner open when we click(tap) on it. And to chcek where the spinner is open or not we can use onItemSelectedListner callbacks. Tell me if you want further code example.

How do I set the spinner value in Kotlin?

This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 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.

like image 150
Tim Kryger Avatar answered Sep 21 '22 19:09

Tim Kryger


To show the Spinner items you just need to call it's performClick() method.

Spinner spDeviceType = (Spinner) findViewById(R.id.spDeviceType); spDeviceType.performClick(); 
like image 23
Amintabar Avatar answered Sep 22 '22 19:09

Amintabar