ICS Spinner (“drop-down” rather than dialog) in older Android versions

可紊 提交于 2019-12-03 14:12:49
chRyNaN

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!