Android Spinner with different layouts for "drop down state" and "closed state"? Android Spinner with different layouts for "drop down state" and "closed state"? android android

Android Spinner with different layouts for "drop down state" and "closed state"?


You have to create a custom Adapter class for the Spinner and overwrite the two methods getView() for the normal closed view and getDropDownView() for the drop down list view. Both methods must return a View object for a single element.

Have a look at this tutorial it might help you getting started.


I was having problem too. Rather than overriding the class, I hava an easier way to do this.

But first you need to understand the difference between the resource id in the adapter constructor, and the other one in setDropDownViewResource(...). For example,

SimpleAdapter adapter =    new SimpleAdapter(ab.getThemedContext(), data, R.layout.actionbar_dropdown, new String[] { "EventID", "Icon" },        new int[] { R.id.event_id, R.id.icon });adapter.setDropDownViewResource(R.layout.actionbar_list_item);

R.layout.actionbar_dropdown is the style for spinner, and R.layout.actionbar_list_item for every single list item.

I used SimpleAdapter here, since if I use ArrayAdapter, the xml can only be a single TextView.

R.layout.actionbar_list_item contains a TextView whose id is event_id and an ImageView whose id is icon.

R.layout.actionbar_dropdown is almost exactly the same as actionbar_list_item, but the visibility of ImageView of the latter is set to GONE.

In this way every list item has a textview and an imageview, but you will only see a textview on the spinner.


Using the code of the tutorial linked by Flo, I created the following CustomSpinnerAdapter in order to show two different sets of strings, one when the items are displayed, and one when not. I hope it helps someone.

public class CustomSpinnerAdapter extends ArrayAdapter<String> {Context mContext;int mTextViewResourceId;String[] mObjects;String[] mShortNameObjects;public CustomSpinnerAdapter(Context context, int textViewResourceId,                            String[] objects, String[] shortNameObjects) {    super(context, textViewResourceId, objects);    mContext = context;    mTextViewResourceId = textViewResourceId;    mObjects = objects;    mShortNameObjects = shortNameObjects;}@Overridepublic View getDropDownView(int position, View convertView,                            ViewGroup parent) {    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    TextView row = (TextView) inflater.inflate(mTextViewResourceId, parent, false);    row.setText(mObjects[position]);    return row;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    return getCustomView(position, convertView, parent);}public View getCustomView(int position, View convertView, ViewGroup parent) {    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    TextView row = (TextView) inflater.inflate(mTextViewResourceId, parent, false);    row.setText(mShortNameObjects[position]);    return row;}}

And the usage inside a Fragment:

CustomSpinnerAdapter mSpinnerAdapter = new CustomSpinnerAdapter(getActivity(), R.layout.spinner_item, getResources().getStringArray(R.array.action_filter), getResources().getStringArray(R.array.action_filter_short_names));

Finally, the layout for the spinner item:

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textSize="18dip"    android:gravity="left"    android:textColor="@color/navdraw_list_item_background_default"    android:padding="5dip" />