1

I have a spinner with the items as below

ArrayAdapter<Message> arrayadapter = new ArrayAdapter<Message>(activity, android.R.layout.simple_spinner_item, messages);
                arrayadapter.setDropDownViewResource(R.layout.textview);

i want to give altenate colors to the listitems.how can i do that.Any help is appreciated

teekib
  • 2,821
  • 10
  • 39
  • 75

3 Answers3

2

You can use

ArrayAdapter<CharSequence> adapter =
    new ArrayAdapter(this, R.layout.simple_spinner_item, myList) {
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        if (position % 2 == 0) { // we're on an even row
           view.setBackgroundColor(evenColor);
        } else {
           view.setBackgroundColor(oddColor);
        }
       return view;
    }
}
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
1

Here are links which might be useful to create a custom spinner of your choice.

1) http://adanware.blogspot.in/2012/03/android-custom-spinner-with-custom.html
2) http://www.edureka.in/blog/custom-spinner-in-android/
3) Android Spinner with different layouts for "drop down state" and "closed state"?

Community
  • 1
  • 1
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

Hi You can use the following code

 @Override
       public View getDropDownView(int position, View convertView, ViewGroup parent){
            View v = convertView;
            if (v == null) {
               Context mContext = this.getContext();
               LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
         } 
    TextView tv=(TextView) v.findViewById(R.id.spinnerTarget);
    tv.setText(testarray.get(position));
    switch (position) {
    case 0:  tv.setTextColor(Color.RED);  
    break; 
    case 1:  tv.setTextColor(Color.BLUE);
    break;
    default:  tv.setTextColor(Color.BLACK);
    break;
    }
return v;  
              }              
       };     
       pSpinner.setAdapter(spinnerAdapter); 
} 
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31