following is demo with spinner with image. and also different color for selected item .
Main spinner layout
spinner item layout
Activity
public class SpinnerWithDiffBG extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bg_simple_spinner);
String[] str = new String[5];
str[0] = "first";
str[1] = "second";
str[2] = "third";
str[3] = "forth";
str[4] = "fifth";
Spinner spinner = (Spinner) findViewById(R.id.ddSample);
// Apply the adapter to the spinner
spinner.setAdapter(new BGSpinnerAdapter(this, str,getResources().getColor(R.color.unknown),getResources().getColor(R.color.red)));
}
}
Spinner Adapter
public class BGSpinnerAdapter implements SpinnerAdapter {
private String[] arrItem;
private int count;
private Context mContext;
private int selectedColor;
private int itemColor;
public BGSpinnerAdapter(Context mContext, String[] arrItem) {
this.mContext = mContext;
this.arrItem = new String[arrItem.length];
this.arrItem = arrItem;
count = arrItem.length;
this.selectedColor = mContext.getResources().getColor(
android.R.color.black);
this.itemColor = mContext.getResources().getColor(R.color.red);
}
public BGSpinnerAdapter(Context mContext, String[] arrItem,
int selectedColor, int itemColor) {
this.mContext = mContext;
this.arrItem = new String[arrItem.length];
this.arrItem = arrItem;
count = arrItem.length;
this.selectedColor = selectedColor;
this.itemColor = itemColor;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position < getCount()) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.bg_spinner_item, parent,
false);
TextView txtSpinnerItem = (TextView) rowView
.findViewById(R.id.txtSpinnerItem);
txtSpinnerItem.setTextColor(selectedColor);
txtSpinnerItem.setText(arrItem[position]);
return rowView;
}
return null;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (position < getCount()) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.bg_spinner_item, parent,
false);
TextView txtSpinnerItem = (TextView) rowView
.findViewById(R.id.txtSpinnerItem);
txtSpinnerItem.setTextColor(itemColor);
txtSpinnerItem.setText(arrItem[position]);
return rowView;
}
return null;
}
}