Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar/ActionBarSherlock multiple choices spinner

I have a ListView containing items which belongs to one or more category. I would like, by clicking on an icon in the actionbar, to select and unselect theses categories. This way, the listView is refresh according to the categories selected.

Here is an example I found :

581753Screenshot20140110103007.pnghttp://www.hostingpics.net/viewer.php?id=581753Screenshot20140110103007.png

For the moment, I found 2 solutions :

  • Adding a spinner with checkable items but it closes the menu at every selection/unselection
  • Create a ListView with cheackable items in a RelativeLayout and make it appears when the icon is clicked.

The second solution fits exactly with the UI expectations but I think there is a sort of multiple choices spinner solution.

like image 892
1MAGIN Avatar asked Jan 10 '14 09:01

1MAGIN


Video Answer


2 Answers

A Spinner shows the drop down using a ListPopupWindow, you could use the same to show that multi choice item selection list:

private void showPopup() {
    final ListPopupWindow lpw = new ListPopupWindow(this);
    lpw.setAdapter(/*Your adapter here*/);
    lpw.setAnchorView(mAnchor); // see below
    lpw.setContentWidth(/*specific value*/); // see below
    lpw.show();
    // this is required because the popup's `ListView` will not be available 
    // until the ListPopupWindow is actually shown.
    mAnchor.post(new Runnable() {
        @Override
        public void run() {
            lpw.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        }
    });
}

You could then call this method from the onOptionsItemSelected() callback when the right MenuItem is selected. There are two other things you need to take care:

The mAnchor is a View that you need to insert in the Activity's layout in the top-right corner so the ListPopupWindow will show in the right position. For example, if you have as an Activity root:

a RelativeLayout then mAnchor will be:

mAnchor = new View(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(0, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mAnchor.setLayoutParams(rlp);
// add mAnchorView first to the RelativeLayout

a LinearLayout then mAnchor will be:

mAnchor = new View(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(0, 0);
llp.gravity = Gravity.RIGHT;
mAnchor.setLayoutParams(llp);
// add mAnchorView first to the LinearLayout(assuming orientation vertical)

and so on for other types of layouts.

Secondly, you need to setup the width of the ListPopupWindow to a desired value. You'll need to adapt this value for different screen sizes and orientation(like phone-portrait and phone-landscape, different table sizes in portrait and landscape).

like image 199
user Avatar answered Oct 28 '22 21:10

user


Original guide available at http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

Adding Drop-down Navigation


Image

Figure 9. A drop-down navigation list in the action bar.

As another mode of navigation (or filtering) for your activity, the action bar offers a built in drop-down list (also known as a "spinner"). For example, the drop-down list can offer different modes by which content in the activity is sorted.

Using the drop-down list is useful when changing the content is important but not necessarily a frequent occurrence. In cases where switching the content is more frequent, you should use navigation tabs instead.

The basic procedure to enable drop-down navigation is:

  1. Create a SpinnerAdapter that provides the list of selectable items for the drop-down and the layout to use when drawing each item in the list.
  2. Implement ActionBar.OnNavigationListener to define the behavior that occurs when the user selects an item from the list.
  3. During your activity's onCreate() method, enable the action bar's drop-down list by calling setNavigationMode(NAVIGATION_MODE_LIST).
  4. Set the callback for the drop-down list with [setListNavigationCallbacks()](http://developer.android.com/reference/android/support/v7/app/ActionBar.html#setListNavigationCallbacks(android.widget.SpinnerAdapter, android.support.v7.app.ActionBar.OnNavigationListener)). For example:

actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);This method takes your SpinnerAdapter and ActionBar.OnNavigationListener.

This procedure is relatively short, but implementing the SpinnerAdapter and ActionBar.OnNavigationListener is where most of the work is done. There are many ways you can implement these to define the functionality for your drop-down navigation and implementing various types of SpinnerAdapter is beyond the scope of this document (you should refer to the SpinnerAdapter class reference for more information). However, below is an example for a SpinnerAdapter and ActionBar.OnNavigationListener to get you started (click the title to reveal the sample).

like image 36
Elad Nava Avatar answered Oct 28 '22 21:10

Elad Nava