Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a second drop down list to Action Bar in Android Honeycomb?

I've been playing with Honeycomb for the last week and had a hard time to find something about more than one drop-down list in Action Bar. I've used this tutorial and successfully added one drop-down list to it. But I don't know how to add another one. Please tell me if you know how to do it. Thanks.

P.S. also one little this how can I change the position of elements in Action Bar(this is off topic)?

like image 482
Alex Bush Avatar asked Dec 21 '22 15:12

Alex Bush


1 Answers

Not sure about Honeycomb but I was able to add two drop down lists to my action bar in JellyBean without using a custom view.

res/menu/main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:id="@+id/menu_main_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:showAsAction="always"/>
  <item
    android:id="@+id/menu_status_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:showAsAction="always"/>
</menu>

MyActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate( R.menu.main_menu, menu );

    MenuItem mainMenuSpinner = menu.findItem( R.id.menu_main_spinner);
    setupMainMenuSpinner( mainMenuSpinner );

    MenuItem statusSpinner = menu.findItem( R.id.menu_status_spinner );
    setupStatusSpinner( statusSpinner );

    return super.onCreateOptionsMenu(menu);
}

private void setupMainMenuSpinner(MenuItem item) {
    View view = item.getActionView();
    if (view instanceof Spinner) {
        Spinner spinner = (Spinner) view;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.main_menu_options,
                android.R.layout.simple_spinner_dropdown_item));
    }
}

private void setupStatusSpinner(MenuItem item) {
    View view = item.getActionView();
    if (view instanceof Spinner) {
        Spinner spinner = (Spinner) view;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.status,
                android.R.layout.simple_spinner_dropdown_item));
    }
}
like image 63
mbonness Avatar answered Jan 11 '23 23:01

mbonness