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:
Thanks
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With