The most elegant and flexible solution I have found so far is here: http://android-er.blogspot.sg/2010/12/custom-arrayadapter-for-spinner-with.html
Basically, follow these steps:
Create custom view class, for your dropdown Adapter. In this custom class, you need to overwrite and set your custom dropdown item layout in getView() and getDropdownView() method. My code is as below:
public class CustomArrayAdapter extends ArrayAdapter<String>{
private List<String> objects;
private Context context;
public CustomArrayAdapter(Context context, int resourceId,
List<String> objects) {
super(context, resourceId, objects);
this.objects = objects;
this.context = context;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public 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) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View row=inflater.inflate(R.layout.spinner_item, parent, false);
TextView label=(TextView)row.findViewById(R.id.spItem);
label.setText(objects.get(position));
if (position == 0) {//Special style for dropdown header
label.setTextColor(context.getResources().getColor(R.color.text_hint_color));
}
return row;
}
}
In your activity or fragment, make use of the custom adapter for your spinner view. Something like this:
Spinner sp = (Spinner)findViewById(R.id.spMySpinner);
ArrayAdapter<String> myAdapter = new CustomArrayAdapter(this, R.layout.spinner_item, options);
sp.setAdapter(myAdapter);
where options is the list of dropdown item string.