Skip to content Skip to sidebar Skip to footer

Listview As Action Overflow In Sherlock Actionbar

How to do get a listdropdown on clicking sherlock action item.It should be similar to creating spinner. But I have a problem with that approach as I dont want the selected item to

Solution 1:

You can create such behavior using Spinner (or IcsSpinner for ActionBarSherlock) in action layout of a menu item. Though you have to use a little trick - hide the currently selected item.

Create menu xml:

<menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/item1"android:actionLayout="@layout/my_dropdown_action_layout"android:showAsAction="always"/>

Where res/layout-v14/my_dropdown_action_layout.xml will contain (this version is used for native action bar):

<?xml version="1.0" encoding="utf-8"?><Spinnerxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="match_parent"android:background="?attr/actionBarItemBackground"android:id="@+id/spinner"/>

and res/layout/my_dropdown_action_layout.xml will contain (this version is used for ActionBarSherlock):

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

Using IcsSpinner is necessary to create a dropdown spinner. If you use res/layout-v14/my_dropdown_action_layout.xml layout for the default version (in res/layout/), it would behave differently on Android 2.x (the spinner would be in dialog mode).

Now you have to fill the spinner with data properly. Just create an Activity where you inflate the menu, this way:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.my_menu, menu);
    MenuItemmenuItem= menu.findItem(R.id.item1);

    ArrayAdapter<String> adapter = newArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, items);
    adapter.setDropDownViewResource(R.layout.list_item);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // native ActionBarSpinnersp= (Spinner) menuItem.getActionView();
        sp.setAdapter(adapter);
    } else {
        // ActionBarSherlockIcsSpinnersp= (IcsSpinner) menuItem.getActionView();
        sp.setAdapter(adapter);
    }

    returnsuper.onCreateOptionsMenu(menu);
}

Now comes the trick of hiding the currently selected item. Layout res/layout/spinner_layout.xml will contain this:

<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@null"><TextViewandroid:layout_width="0dp"android:layout_height="0dp"android:id="@+id/text"android:visibility="invisible"/><ImageViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:src="@drawable/my_dropdown_icon"android:background="@null"/></FrameLayout>

This way you see an icon as the menu item and you have the dropdown menu. Note that layout res/layout/list_item.xml has to contain a TextView with id R.id.text too.

Alternatively, you can use similar approach where you can use ActionProvider instead of action layout.

And another solution would be to create custom widget similar to dropdown Spinner.

Post a Comment for "Listview As Action Overflow In Sherlock Actionbar"