Search code examples
androidactionbarsherlock

More than one CustomView in ActionBar


I finally managed to get an ICS styled spinner into an Android 2.3.x ActionBar of my Tabbed Navigation Sherlock Fragment using this code:

ActionBar bar = getSherlockActivity().getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    int dropDownStyle = R.attr.actionDropDownStyle;

    ArrayAdapter<String> someAdapter = new ArrayAdapter<String>(getSherlockActivity()
            .getSupportActionBar().getThemedContext(), R.layout.sherlock_spinner_dropdown_item,
            new String[] {
                    "Last 7 days", "Last month", "Last 6 months", "Last year"
            });

    IcsSpinner mySpinner = new IcsSpinner(getActivity(), null, dropDownStyle);
    mySpinner.setAdapter(someAdapter);
    mySpinner.setOnItemSelectedListener(new IcsAdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(IcsAdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                //do stuff
            }

        }

        @Override
        public void onNothingSelected(IcsAdapterView<?> parent) {
            // simulate a click on the first item of the spinner
            //do stuff

        }

    });
    bar.setCustomView(mySpinner);
    bar.setDisplayShowCustomEnabled(true);

Which displays like so:

first tab showing ics spinner in 2.3.x

But the next tab along needs to have 2 ICS spinners in the ActionBar. (Previous to using ABS, I had a spinner in the Activity and the options for the second spinner were in a standard options menu.)

When I try to add the second CustomView, it overwrites (replaces? overdraws?) the first one (I add both in the second fragment, with different names), like so:

second tab showing just 1 spinner

Is it possible to have 2 CustomViews in the ActionBar, or am I barking up the wrong tree? How, then, to achieve 2 ICS spinners in the ActionBar?


Solution

  • As per earlier comment:

    In order to add multiple views, you should just be able to wrap them in a container, and then set that container as the custom view to the ActionBar. I.e. a horizontal LinearLayout sounds like a good candidate.