Android Custom Spinner and its drop-down window

Source: Internet
Author: User
Tags getcolor

Custom spinner actually consists of two parts:

The first part is used to open the drop-down list of buttons, this green background directly set spinner background on the line, the material file as follows:

Note that the text in the spinner control does not directly modify the text interface, the text is actually set in the adapter, for example:

arrayadapter<string> adapter = new Arrayadapter<string> (GetContext (),

r.layout.spinner_checked_text, gradelist);

Gradespinner.setadapter (adapter);

The implementation of this spinner_checked_text.xml is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Checkedtextviewxmlns:android= "Http://"Android:id= "@android: Id/text1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center_vertical"android:padding= "10DP"Android:singleline= "true"Android:textcolor= "@color/text_green"android:textsize= "20SP" ></Checkedtextview>

It must be checkedtextview, or it will go wrong.

The second part is the pull-out window, this is the Getdropdownview () method of the replication Arrayadapter to achieve, the code is as follows:

arrayadapter<string> adapter =NewArrayadapter<string>(GetContext (), R.layout.spinner_checked_text, gradelist) {@Override PublicView Getdropdownview (intposition, View Convertview, ViewGroup parent) {View View=Inflate (GetContext (), R.layout.spinner_item_layout,NULL); TextView label=(TextView) view. Findviewbyid (; ImageView Check=(ImageView) view. Findviewbyid (;                Label.settext (Gradelist.get (position)); if(gradespinner.getselecteditemposition () = =position)                    {View.setbackgroundcolor (Getresources (). GetColor (R.color.spinner_green));                Check.setimageresource (r.drawable.check_selected); } Else{View.setbackgroundcolor (Getresources (). GetColor (R.color.spinner_ligh                    T_green));                Check.setimageresource (R.drawable.check_unselect); }                returnview;        }        }; Adapter.setdropdownviewresource (r.layout.spinner_item_layout);

The contents of Spinner_item_layout.xml are as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/spinner_light_green"android:padding= "15DP" >    <TextViewAndroid:id= "@+id/spinner_item_label"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"Android:textcolor= "@color/text_green"android:textsize= "17SP" />    <ImageViewAndroid:id= "@+id/spinner_item_checked_image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true"android:src= "@drawable/check_unselect" /></Relativelayout>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.