ICS Spinner ("drop-down", not a dialog) in older versions of Android

I'm trying to imitate the new version of Ice Cream Sandwhich spinner, where it looks more like a drop-down list rather than a pop-up dialog. Check out this link to understand what I'm talking about. I read several posts about using ActionBarSherlock to get the desired effect. However, this is for use only in the action bar, so how can I extract ICS Spinner from the action bar?

There is a pretty good answer here, do I feel it went a little too far? Is there an easier way?

+6
source share
1 answer

First, I referred to this link to answer or not to answer my own question. I felt that this could be very useful for someone facing a similar problem, so I apologize if this is not the right etiquette for this site (to answer your own question).

Now I came across an attempt to find a solution to this problem, and with trial and error I succeeded. So, as soon as you download and configure the ActionBarSherlock SDK in your project, create your own layout, which will include a 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 code above will use the version of the ICS counter, which is located in the ActionBarSherlock library. Then, in your activity, declare and instantiate (using casting) the spinner object. But keep in mind that you are not using the regular Spinner class, you are using the IcsSpinner class found in the ActionBarSherlock library:

 IcsSpinner spinner = (IcsSpinner)findViewById(R.id.spinner); 

Now you create the adapter in the same way as for the usual Spinner, for example:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, elements); spinner.setAdapter(adapter); 

Finally, you need to configure onItemSelectedListener . The only significant difference here is that you are using IcsAdapterView.OnItemSelectedListener , not 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){ } }); 

What is it. It really is not that much different, just using a spinner object. Whatever it was, it took me some time to understand, so I hope this is useful to someone.

Oh yes, and don't forget to use the ActionBarSherlock theme, for example (in the manifest):

 android:theme="@style/Theme.Sherlock" 
+17
source

All Articles