Android Searchview custom Searchicon and font color size

Source: Internet
Author: User

The custom Searchview search icon and font properties are relatively complex and are noted.

One, custom Searchicon

1, API version is less than 21: version is less than 21 o'clock, to modify Searchicon more complex, you need to get to Searchview ImageView, and then set up a picture for ImageView, the specific code is as follows:

(1) Initializing the Searchview control

Msearch = (Searchview) View.findviewbyid (R.id.search);

(2) Setting a custom Search icon

if(msearch==NULL){    return;}Else{//gets the ID of the ImageViewintImgid = Msearch.getcontext (). Getresources (). Getidentifier ("Android:id/search_mag_icon",NULL,NULL); //Get ImageViewImageView SearchButton =(ImageView) Msearch.findviewbyid (imgid);//set up a pictureSearchbutton.setimageresource (r.drawable.search);//do not use the defaultMsearch.seticonifiedbydefault (false);}
2, API version is more than 21 o'clock, it is very convenient, directly in the layout file for Searchview set properties Searchicon can be.
android:searchicon= "@drawable/search"
Second, custom font color and size, you can also modify the color of the hint text in Searchview

1, initialize the Searchview control, ibid.

2, get to Searchview TextView, then you can modify its properties, the code is as follows.

if(msearch==NULL){    return;}Else{//gets the ID to the TextViewintid = Msearch.getcontext (). Getresources (). Getidentifier ("Android:id/search_src_text",NULL,NULL);//get the control to TextViewTextView TextView =(TextView) Msearch.findviewbyid (ID);//set font size to 14SPTextview.settextsize (TYPEDVALUE.COMPLEX_UNIT_SP, 14);//14SP//Set Font ColorTextview.settextcolor (Getactivity (). Getresources (). GetColor (R.color.search_txt_color));//set hint text colorTextview.sethinttextcolor (Getactivity (). Getresources (). GetColor (R.color.search_hint_color));}
Third, the final:

Android Searchview custom Searchicon and font color size

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: info-contact@alibabacloud.com 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.