Skip to content Skip to sidebar Skip to footer

How To Create Custom Drop Down Menu With Actionbarsherlock?

So, I'm trying to mimic something like Pandora's menu: Where you have the choice to hit the menu button or icon in the top right corner, either way, it will show the same drop dow

Solution 1:

With some experimentation I managed to get part of what I wanted accomplished. I'm not sure if this is the best approach but it did work.

I found out that the reason why the spinner would not display at the far right of the screen is because that space is reserved for menu items. If you remember, the original reason why I didn't use menu items is because on older devices when you hit the menu button it would display the items at the bottom of the screen (not what I wanted). But I found a work around.

I first created my menu in XML:

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/spinner_menu_item"android:showAsAction="always"android:actionLayout="@layout/spinner" /></menu>

and "@layout/spinner":

<?xml version="1.0" encoding="utf-8"?><com.actionbarsherlock.internal.widget.IcsSpinnerxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/spinner_menu"android:layout_width="wrap_content"android:layout_height="wrap_content"  
    />

then in the onCreateOptionsMenu:

    getSupportMenuInflater().inflate(R.menu.basic_menu, menu);
    MenuItemitem= menu.findItem(R.id.spinner_menu_item);
    spinnerMenu = (IcsSpinner)item.getActionView();
    ArrayAdapter<String> adapter = newArrayAdapter<String>(MyActivity.this, android.R.layout.simple_dropdown_item_1line, menuItems);
    spinnerMenu.setAdapter(adapter);
    spinnerMenu.setOnItemSelectedListener(newIcsAdapterView.OnItemSelectedListener() {}

and finally I overrided the menu button, like so:

@OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_MENU){
        spinnerMenu.performClick();
        returntrue;
    }
    returnsuper.onKeyDown(keyCode, event);
}

The only thing this doesn't do is display an icon rather than the words. But half way there! I hope this will help someone faced with a similar problem.

Post a Comment for "How To Create Custom Drop Down Menu With Actionbarsherlock?"