I'm trying to mimic the newer Ice Cream Sandwhich version of the spinner, where it looks more like a drop-down list rather than a pop-up dialog. See this link to get an idea of what I'm talking about. I read some posts stating to use ActionBarSherlock to get the desired effect. However, this is created for use in the action bar only, so, how do I take the ICS Spinner out of the action bar?
There is a pretty good answer here, yet, I feel like this is a little bit overkill? Is there an easier way?
First off, I referenced this link to whether or not I should answer my own question. I felt this can be very useful to someone faced with a similar problem, so I apologize if this is not proper etiquette for this website (to answer your own question).
Now, I have stumbled around trying to find a solution for this problem and with trial and error I have succeeded. So, once you have ActionBarSherlock SDK downloaded and set up in your project, create your layout that will incorporate the spinner:
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_margin="10sp"
android:layout_centerHorizontal="true"
android:textSize="18sp" />
The above code will use the ICS version of the spinner which is in the ActionBarSherlock library. Next, in your Activity declare and instantiate (using casting) the spinner object. But note that you do not use the normal Spinner class, you use the IcsSpinner class found in the ActionBarSherlock library:
IcsSpinner spinner = (IcsSpinner)findViewById(R.id.spinner);
Now, you create an adapter just as you would for the normal Spinner, like so:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, elements);
spinner.setAdapter(adapter);
Finally, you need to set up the onItemSelectedListener
. The only major difference here is that you use IcsAdapterView.OnItemSelectedListener
rather than just OnItemSelectedListener
:
spinner.setOnItemSelectedListener(new IcsAdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(IcsAdapterView<?> parent, View view, int position, long id){
}
@Override
public void onNothingSelected(IcsAdapterView<?> parent){
}
});
And that's it. It's really not much different then just using the spinner object.
As easy as it is, it took me awhile to figure out, so, I hope this is useful to someone.
Oh yeah, and don't forget to use an ActionBarSherlock theme, like so (in the manifest):
android:theme="@style/Theme.Sherlock"