Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can change header of dropdown view in android?

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;
        }
    }
like image 744
mukesh Avatar asked Oct 08 '12 08:10

mukesh


2 Answers

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.

like image 185
Vishal Pawar Avatar answered Oct 08 '22 19:10

Vishal Pawar


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

like image 39
SuReSh PaTi Avatar answered Oct 08 '22 20:10

SuReSh PaTi