Search code examples
androidandroid-actionbaractionbarsherlockandroid-theme

Dropdown Spinner outside of actionbar? (IceCream Sandwich style, w/ActionBarSherlock)


Is there a way to create a Dropdown Spinner for Android 2.3.3? I am using ActionbarSherlock.

Here is an Example of what I mean:

enter image description here

Thanks


Solution

  • As it stands, you're in luck. It can be done with ActionBarSherlock and it works with versions pre-4.0 . However, I'm not 100% sure Jake Wharton will want us to use it like this, since it's not exactly "public api", AFAIK (I've meant to ask). Anyway, you have to first create your own class to extend from the ActionBarSherlock class:

    public class MyIcsSpinner extends IcsSpinner {
    
      public MyIcsSpinner(Context context, AttributeSet attrs) {
        super(context, attrs, com.actionbarsherlock.R.attr.actionDropDownStyle);
    
      }
    
      public MyIcsSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    
      }
    }
    

    To include it in a layout:

    <com.blah.blah.blah.MyIcsSpinner
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:textAllCaps="true"
        android:background="@drawable/abs__spinner_ab_holo_light"
        android:textColor="#000000"
        android:gravity="center"/>
    

    Now you have to create a custom SpinnerAdapter, and you need to override the following methods to get the proper look and feel:

    @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        final TextView filterName;
        if (convertView == null) {
          filterName = (TextView) layoutInflater.inflate(R.layout.filter_item, parent, false);
        } else {
          filterName = (TextView) convertView;
        }
    
        filterName.setText(getItem(position));
        return filterName;
      }
    
      @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent) {
        final TextView filterName;
        if (convertView == null) {
          filterName = (TextView) layoutInflater.inflate(R.layout.sherlock_spinner_dropdown_item, parent, false);
          filterName.setEllipsize(TruncateAt.END);
        } else {
          filterName = (TextView) convertView;
        }
    
        filterName.setText(getItem(position));
        return filterName;
      }
    

    YMMV, esp. regarding the themes.