5

I have a spinner in my layout & set the prompt with android:prompt="@string/year".

The default prompt looks like:

Default prompt looks as

Now my questions are:

  1. How do I change the default icon to another image?
  2. How to get the "Year" text appear in center with red color?
  3. How to change background color to yellow (background in which 'Year' text & icon exists)?

My custom Adapter class

private class MyCustomSpinnerAdapter extends ArrayAdapter<ArrayList> {
        private ArrayList<String> objectsList = new ArrayList<String>();

        @SuppressWarnings("unchecked")
        public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
                ArrayList objects) {
            super(context, textViewResourceId, objects);
            this.objectsList = objects;
        }

        @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 getCustomView1(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
                    false);
            LinearLayout rowTitle = (LinearLayout) rowView
                    .findViewById(R.id.row);

            rowTitle.setBackgroundResource(R.drawable.spinner_row_focused);

            TextView textView = (TextView) rowView.findViewById(R.id.title);
            textView.setTypeface(typeFace);
            textView.setText(objectsList.get(position).toString().trim());
            return rowView;
        }

        public View getCustomView1(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
                    false);

            TextView textView = (TextView) rowView.findViewById(R.id.title);
            textView.setText(objectsList.get(position).toString().trim());
            textView.setTypeface(typeFace);
            return rowView;
        }
    }
Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
mukesh
  • 4,140
  • 5
  • 29
  • 40

5 Answers5

3

Here is your solution: Create simple layout for header (file R.layout.spinner_head):

<ImageView
    android:id="@+id/spin_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" 
    android:padding="5dip"
    android:src="@android:drawable/btn_star"
    android:layout_alignParentLeft="true"/>

<TextView
    android:id="@+id/title"
    android:textColor="#f00"
    android:gravity="center"
    android:text="Year"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" 
    />

Create custom spinner:

class MySpinner extends Spinner {

        public MySpinner(Context context) {
            super(context);
        }

        @Override
        public boolean performClick() {
            boolean handled =  false;//super.performClick();

            if (!handled) {

                handled = true;

                Context context = getContext();

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                CharSequence mPrompt = getPrompt();
                if (mPrompt != null) {

                    builder.setCustomTitle(getLayoutInflater().inflate(R.layout.spinner_head, null));

                }

                builder.setSingleChoiceItems(new DropDownAdapter(getAdapter()),getSelectedItemPosition(), this).show();

            }

            return handled;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            setSelection(which);
            dialog.dismiss();
        }

        class  DropDownAdapter implements ListAdapter, SpinnerAdapter {
            private SpinnerAdapter mAdapter;

            public  DropDownAdapter(SpinnerAdapter adapter) {
                this.mAdapter = adapter;
            }

            @Override
            public int getCount() {
                return mAdapter == null ? 0 : mAdapter.getCount();
            }

            @Override
            public Object getItem(int position) {
                return mAdapter == null ? null : mAdapter.getItem(position);
            }

            @Override
            public long getItemId(int position) {
                return mAdapter == null ? -1 : mAdapter.getItemId(position);
            }

            @Override
            public int getItemViewType(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return getDropDownView(position, convertView, parent);
            }

            @Override
            public int getViewTypeCount() {
                return 1;
            }

            @Override
            public boolean hasStableIds() {
                return mAdapter != null && mAdapter.hasStableIds();
            }

            @Override
            public boolean isEmpty() {
                return getCount() == 0;
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                 if (mAdapter != null) {
                     mAdapter.registerDataSetObserver(observer);
                 }

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                 if (mAdapter != null) {
                     mAdapter.unregisterDataSetObserver(observer);
                 }

            }

            @Override
            public View getDropDownView(int position, View convertView,
                    ViewGroup parent) {
                return mAdapter == null ? null : mAdapter.getDropDownView(position, convertView, parent);
            }

            @Override
            public boolean areAllItemsEnabled() {
                return true;
            }

            @Override
            public boolean isEnabled(int arg0) {
                return true;
            }


        }
    }

Create instance of your spinner

MySpinner theSpin = new MySpinner(this);
            theSpin.setPrompt("Select Item");
            ArrayAdapter<String> theAdapter = 
                new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[]{"item1","item2","item3","item4","item5","item6" });
            theSpin.setAdapter(theAdapter);

Now use this custom spinner which will show custom header. You can modify that header as per your needs.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
1

I think u better to prepare custom spinner like custom ListView and check this link http://android-er.blogspot.in/2010/12/custom-spinner-with-icon.html

SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46
  • Thanks for answering,but i not looking for custom adapter for spinner.Its change the row of spinner.I am looking for changing header of list. – mukesh Oct 08 '12 at 09:13
  • part of images that shows by android:prompt="@string/year" code. – mukesh Oct 08 '12 at 09:16
1

Here just change the name spinner's set prompt and pass the name as a string there,below i am giving the syntax look like so here it is-

spinner.setPrompt("Name of Your Header");
Ravi
  • 2,277
  • 3
  • 22
  • 37
0

For changing the color of your text, you could try to use HTML

String redText = "This is <font color='red'>Red text</font>.";
mSpinner.setPrompt(Html.fromHtml(redText));

You can try to hack the header along the lines that this post suggests How can I customize the header layout of a dialog

Window window = mSpinner.getWindow();
window.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_header);

Where R.layout.my_custom_header is the layout in which you define what you want to appear in your header. I have not tried this, just going off of what was suggested in the post i referenced above.

But probably your best bet to is to listen to Chiru and make your own custom view to have it look exactly like what you want.

Community
  • 1
  • 1
Akos Cz
  • 12,711
  • 1
  • 37
  • 32
0

I looked at the Spinner class code, there the DialogPopup is a inner private class. This class encapsulates the AlertDialog and ListView shown in a Spinner dialog. It is private so we cannot extend it.

Here is an answer that might help you : How do I create an Android Spinner as a popup?

In addition to the code in that accepted answer, call setCustomTitle on the dialog to have your own style title view OR find the id of the icon view in the AlertDialog xml layout and set the icon directly if you only want to change the icon.

Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97