Skip to content Skip to sidebar Skip to footer

How To Customize The Spinner Dropdown View

Is it possible to customize the spinner drop-down view . Default spinner drop-down view has adapter view . I want change that view to have my own text view or some thing like this

Solution 1:

Add this inner class in your class and modify it as you please.

publicclassMyAdapterextendsArrayAdapter<String>{

        publicMyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Overridepublic View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Overridepublic 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.xml.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.company);
            label.setText(strings[position]);

            TextView sub=(TextView)row.findViewById(R.id.sub);
            sub.setText(subs[position]);

            ImageView icon=(ImageView)row.findViewById(R.id.image);
            icon.setImageResource(arr_images[position]);

            return row;
            }
        }

And add this adapter to your spinner:

SpinnermySpinner= (Spinner)findViewById(R.id.expandableImagesList);
    mySpinner.setAdapter(newMyAdapter(NewEventActivity.this, R.xml.row, strings));

And also create a new xml and add all the things you want your spinner to contain:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="3dip"
><ImageViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/icon"/><TextViewandroid:layout_toRightOf="@+id/image"android:padding="3dip"android:layout_marginTop="2dip"android:textStyle="bold"android:id="@+id/company"android:text="CoderzHeaven"android:layout_marginLeft="5dip"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:layout_toRightOf="@+id/image"android:padding="2dip"android:layout_marginLeft="5dip"android:id="@+id/sub"android:layout_below="@+id/company"android:text="Heaven of all working codes"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout>

This is my code so you have to change it for your needs

Solution 2:

You can customize the drop-down view by overriding getDropDownView method from your ArrayAdapter.

@Overridepublic View getDropDownView(int position, View convertView, ViewGroup parent){
     ...
}

Solution 3:

You can do

arrayAdapter.setDropDownViewResource(R.layout.your_layout);

where your_layout is your XML containing only a TextView

Post a Comment for "How To Customize The Spinner Dropdown View"