Android SearchView custom SearchIcon and font color size, searchviewicon

Source: Internet
Author: User
Tags getcolor

Android SearchView custom SearchIcon and font color size, searchviewicon

The search icons and font attributes of custom SearchView are more complex.

1. Custom SearchIcon

1. If the API version is earlier than 21: if the version is earlier than 21, it is complicated to modify the SearchIcon. You must first obtain the ImageView of the SearchView and then set the image for the ImageView. The Code is as follows:

(1) initialize the SearchView Control

mSearch = (SearchView) view.findViewById(R.id.search);

(2) Set Custom Search icons

If (mSearch = null) {return;} else {// obtain the ImageView idint imgId = mSearch. getContext (). getResources (). getIdentifier ("android: id/search_mag_icon", null, null); // obtain ImageViewImageView searchButton = (ImageView) mSearch. findViewById (imgId); // sets the image searchButton. setImageResource (R. drawable. search); // do not use the default mSearch. setIconifiedByDefault (false );}
2. When the API version is greater than 21, it is very convenient to directly set the property searchIcon for SearchView in the layout file.
android:searchIcon="@drawable/search"
2. Customize the font color and size. You can also modify the color of the prompt text in SearchView.

1. initialize the SearchView control.

2. Get the TextView of SearchView and modify its attributes. The Code is as follows.

If (mSearch = null) {return;} else {// obtain the TextView IDint id = mSearch. getContext (). getResources (). getIdentifier ("android: id/search_src_text", null, null); // obtain the TextView control TextView textView = (TextView) mSearch. findViewById (id); // set the font size to 14sptextView. setTextSize (TypedValue. COMPLEX_UNIT_SP, 14); // 14sp // set the font color textView. setTextColor (getActivity (). getResources (). getColor (R. color. search_txt_color); // sets the text color textView. setHintTextColor (getActivity (). getResources (). getColor (R. color. search_hint_color ));}
Iii. Final:

 

 

 

 

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: 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.