Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Open Spinner from Button

In an Android application, is it possible to open the spinner popup from a button click instead of pressing the actual spinner?

I have tried the following:

Button btnChange = (Button)findViewById(R.id.btnChange);
            btnChange.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
                     Spinner mySpinner = (Spinner) findViewById(R.id.sSpinner);
                     mySpinner.showContextMenu();
                  }
                });
like image 981
Zeno Avatar asked Jan 29 '11 19:01

Zeno


3 Answers

Try this one:

Spinner mySpinner = (Spinner) findViewById(R.id.sSpinner);
Button btnChange = (Button)findViewById(R.id.btnChange);    
btnChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mySpinner.performClick();
                }
            });
like image 84
DareDeviL Avatar answered Oct 16 '22 12:10

DareDeviL


Sorry for late answer - it's possible:

((Spinner) findViewById(R.id.mySpinner)).performClick();
like image 33
zeratul021 Avatar answered Oct 16 '22 14:10

zeratul021


It is possible, you just call it popup menu, not spinner.

ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView .setOnClickListener(new OnClickListener(){
            PopupMenu pum = new PopupMenu(this, findViewById(R.id.image));
            pum.inflate(R.menu.image_chooser_popup);
            pum.show();

          });
}

Your spinner (or popup) items goes to R.menu.image_chooser_popup:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:title="take a picture"
    android:titleCondensed="camera"
    android:visible="true"
    android:onClick="cameraIntent" />
<item
    android:title="choose picture from gallery"
    android:titleCondensed="string"
    android:visible="true"
    android:onClick="galleryIntent"/>

Hope this one helps some one. If you have any issues with my responce, please fill free to ask.

like image 41
Defuera Avatar answered Oct 16 '22 14:10

Defuera