Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 977
Ahmad Avatar asked Apr 25 '12 20:04

Ahmad


2 Answers

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.

like image 72
dmon Avatar answered Oct 12 '22 13:10

dmon


I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)

The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    SubMenu sub = menu.addSubMenu("More");
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    sub.getItem().setIcon(R.drawable.ic_menu);

    getSupportMenuInflater().inflate(R.menu.activity_main, sub);

    return true;
}

Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.

It works!

like image 24
Yingyi Xu Avatar answered Oct 12 '22 11:10

Yingyi Xu