Saturday, December 25, 2010

Custom ArrayAdapter for Spinner, with different icons

In last exercise "Custom Spinner with icon", a custom spinner have been implemented. But all row have the same icon. In order to display a spinner with difference icons on each row, we can implement our own ArrayAdapter, override getDropDownView() and getView() methods.

Custom ArrayAdapter for Spinner, with different icons

row.xml and main.xml keep no change as in the exercise "Custom Spinner with icon".

AndroidCustomSpinner.java
package com.exercise.AndroidCustomSpinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidCustomSpinner extends Activity {

String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
   mySpinner.setAdapter(new MyCustomAdapter(AndroidCustomSpinner.this, R.layout.row, DayOfWeek));
}

public class MyCustomAdapter extends ArrayAdapter<String>{

public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);

LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.weekofday);
label.setText(DayOfWeek[position]);

ImageView icon=(ImageView)row.findViewById(R.id.icon);

if (DayOfWeek[position]=="Sunday"){
icon.setImageResource(R.drawable.icon);
}
else{
icon.setImageResource(R.drawable.icongray);
}

return row;
}
}
}
***********
It's a programmatic pitfall here:
in getCustomView(), Condition checking of String ("Sunday") should be checked with:
(DayOfWeek[position].equals("Sunday"))

"==" not always work! refer String Comparison: equals()? ==?
***********



download filesDownload the files.


* Please see the article "In-correct arrow icon on Spinner with custom ArrayAdapter"!

Reposted: Custom ArrayAdapter for Spinner, with custom icons.

9 comments:

green rise said...

Is it possible to have 2 rows for each row of spinner.?

Unknown said...

Any idea on how to get the radio buttons back?

Erik said...

hello Vic,

Do you means the In-correct arrow icon on Spinner? I have no any idea right now!

ElectricSheep said...

huh??
you compare strings with a "==" ??

Erik said...

hello ElectricSheep,

thx for your reminding. equals() should be used. Updated with note.

Lauyo said...

Very useful peace of code...i am gratefull to you, thank you

Unknown said...

thank you very much! I really needed this information. But I only have a problem when I select a element from the list, in the main spinner button changes the text but doesn't change the icon...do you know any solution? thank you very much, and sorry for my English gramatic, I'm not a native English person ;-)

Erik said...

hello Fidel López Gómez,

I re-check it again and found it work as expect. Or you can download my APK to check it again on your device. Please refer : Custom ArrayAdapter for Spinner, with custom icons

Anonymous said...

Is it possible to set the height of the spinner dropdown ?