Customizing a spinner in android.

By | July 18, 2011

Hello everyone………

You all knew that a spinner or combobox is an inbuilt widget in android. And Like any other widgets spinners are also customizable.
Here is a simple example to customize a spinner. First we will look at the java code.
The getView method is called for each row in the spinner. So with the help of an Layout Inflater you can inflate any layout for each row.
At extreme you can have each layout for each row.

Check these older posts about spinner.

1. How to get a selected Item from a spinner in ANDROID?

2. How to set an item selected in Spinner in ANDROID?

3. How to create a custom ListView in android?

Check out these of ListViews

1. Simplest Lazy Loading ListView Example in Android with data populated from a MySQL database using php.

2. How to add checkboxes and radio buttons to ListView in android? OR How to set single choice items in a ListView in android?

Custom Spinner Demo

Custom Spinner Demo

Custom Spinner Demo

Custom Spinner Demo

Now we will look at the source code

Resources needed.
These are images I am using in this example. Put these images inside the res/drawable folder.


package         pack.coderzheaven;

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 CustomSpinnerDemo extends Activity {
	String[] strings = {"CoderzHeaven","Google",
			"Microsoft", "Apple", "Yahoo","Samsung"};

	String[] subs = {"Heaven of all working codes ","Google sub",
			"Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};

	int arr_images[] = { R.drawable.coderzheaven,,,,,};
    public void onCreate(Bundle savedInstanceState) {

        Spinner mySpinner = (Spinner)findViewById(;
        mySpinner.setAdapter(new MyAdapter(CustomSpinnerDemo.this, R.layout.row, strings));

    public class MyAdapter extends ArrayAdapter{

    	public MyAdapter(Context context, int textViewResourceId,	String[] objects) {
    		super(context, textViewResourceId, objects);

    	public View getDropDownView(int position, View convertView,ViewGroup parent) {
    		return getCustomView(position, convertView, parent);

    	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=getLayoutInflater();
	    	View row=inflater.inflate(R.layout.row, parent, false);
	    	TextView label=(TextView)row.findViewById(;

	    	TextView sub=(TextView)row.findViewById(;

	    	ImageView icon=(ImageView)row.findViewById(;

	    	return row;

Now the main.xml which defines the main layout.

Now the layout for each row in the spinner.

Now the strings.xml.

CustomSpinner Demo from CoderzHeaven!  Select your Favourite  CustomSpinner Demo#ffffff#000000#347C2C#FF00FF#a020f0#778899#C11B17#FFFF8C#b0e0e6#2F1700#7D2252Select Category#606060

Please click the +1 button on top to share it with your friends and leave your valuable comments also.

Leave a Reply

Your email address will not be published. Required fields are marked *