Android advanced: dynamically modify the icon of the android built-in Search dialog box (floating search)

Source: Internet
Author: User

The last time I wrote about the configuration of building the built-in floating search box for Android, it was quite simple. It was just a bit of trouble. During my demo writing process, I found a problem that was mentioned last time.

 



Here is an android: icon = "@ drawable/menu_route". I thought it was possible to search for the view in front of text and then found that it did not work, it seems useless because this attribute is not mentioned in the document.

 

Effect

 

This is the case. I read the SDK documentation and found that the configuration of the modified icon is not provided here. What should I do if I need to change it here? So I have studied the source code and finally solved it.

 

Take a closer look at the icon on both sides of the intermediate input box and you can see that the icon on the left is an imageview, And the icon on the right is a fixed button.

 

This configuration is in

In the/RES/layout/search_bar.xml file, you can view the layout as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <view Android: Orientation = "vertical" Android: Id = "@ ID/search_bar" Android: focusable = "true" Android: descendantfocusability = "afterdescendants" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" class = "android. app. searchdialog $ searchbar "<br/> xmlns: Android:" http://schemas.android.com/apk/res/android "> <br/> <linearlayout Android: Orientation =" vertical "Android: Id =" @ ID/search _ Plate "Android: Background =" @ drawable/search_plate_global "Android: paddingleft =" 12.0dip "Android: paddingtop =" 7.0dip "Android: paddingright =" 6.0dip "Android: paddingbottom = "16347dip" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <br/> <textview Android: textappearance = "? Textappearancesmall "Android: textcolor = "? Textcolorprimaryinverse "Android: Id =" @ ID/search_badge "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_marginbottom =" 2.0dip "Android: drawablepadding = "0.0dip"/> <br/> <linearlayout Android: Orientation = "horizontal" Android: Id = "@ ID/search_edit_frame" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <br/> <imageview Android: layout_gravity = "center_vertical" Android: Id = "@ ID/search_app_icon" Android: layout_width = "361_dip" Android: layout_height = "361_dip" Android: layout_marginright = "7.0dip"/> <br/> <view Android: ellipsize = "end" Android: Id = "@ ID/search_src_text" Android: background = "@ drawable/textfield_search" Android: paddingleft = "8.0dip" Android: paddingright = "6.0dip" Android: layout_width = "0.0dip" Android: layout_height = "wrap_content" Android: singleline = "true" Android: drawablepadding = "2.0dip" Android: popupbackground = "@ drawable/search_dropdown_background" Android: layout_weight = "1.0" Android: inputtype = "textautocomplete" Android: dropdownwidth = "fill_parent" Android: dropdownanchor = "@ ID/search_plate" Android: dropdownheight = "fill_parent" Android: dropdownverticaloffset = "-9.0dip" class = "android. app. searchdialog $ searchautocomplete "/> <br/> <button Android: Id =" @ ID/search_go_btn "Android: Background =" @ drawable/btn_search_dialog "Android: layout_width = "wrap_content" Android: layout_height = "fill_parent"/> <br/> <imagebutton Android: Id = "@ ID/search_voice_btn" Android: background = "@ drawable/resize" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: layout_marginleft = "0.0dip" Android: layout_margintop = "-6.5dip" Android: layout_marginright = "-5.0dip" Android: layout_marginbottom = "-7.0dip" Android: src = "@ drawable/ic_btn_speak_now"/> <br/> </linearlayout> <br/> </View>

 



We can see that Google has also customized a search box for Android. App. searchdialog $ searchautocomplete without the use of autocompletetextview.

 

The above is the layout file in the search box, which shows that searchdialog has not seen this class in the Android. app package at the beginning. I don't know what's going on.

 

The method for calling the search box is throughOnsearchrequested

() To call the startsearch ()

 

In acitivity, we can see that the variable msearchmanager actually calls its method startsearch ()

 

In this way, we will understand the entire call process of the search box.

 

In my applications:

 

Mapactivity -- "acitalog --" searchmanager -- "searchdialog

 

This makes it clear that the following operations can be performed. However, the global variables in these classes arePrivate

And the set method is not provided, so we cannot directly access the variable for modification. There is only one method.Reflection

 

First look at the code: After the startsearch () method is called to display the search box

 

Try {<br/> // modify the search box icon through reflection <br/> class activityclass = Class. forname ("android. app. activity "); <br/> Field activityclassf = activityclass. getdeclaredfield ("msearchmanager"); <br/> activityclassf. setaccessible (true); <br/> Object msearchmanager = activityclassf. get (this); </P> <p> class searchclass = Class. forname ("android. app. searchmanager "); <br/> Field msearchdialogf = searchclass. getdeclaredfield ("msearchdialog"); <br/> msearchdialogf. setaccessible (true); <br/> Object msearchdialog = msearchdialogf. get (msearchmanager); </P> <p> class searchdialog = Class. forname ("android. app. searchdialog "); <br/> Field mappicon = searchdialog. getdeclaredfield ("mappicon"); <br/> mappicon. setaccessible (true); <br/> imageview = (imageview) mappicon. get (msearchdialog); <br/> Bitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. test); <br/> imageview. setimagedrawable (New bitmapdrawable (Bitmap); </P> <p >}catch (exception e) {<br/> E. printstacktrace (); <br/>}

 

Here, we first get the object of the private variable msearchmanager in the current object acitivity and then get the searchdialog object of the private variable msearchdialog through it, finally, we get the private variable mappicon in searchdialog and the imageview. Of course we can modify it.

 

The above is the one on the right of the icon method on the left of the search box, which can also be modified, but it is a button.

 

Field mgobuttonf = searchdialog. getdeclaredfield ("mgobutton"); <br/> mgobuttonf. setaccessible (true); <br/> button mgobutton = (button) mgobuttonf. get (msearchdialog); <br/> bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. transit); <br/> mgobutton. setcompounddrawableswithintrinsicbounds (New bitmapdrawable (Bitmap), null, null );

 

In this way, the modification is completed and the following figure shows the effect:

 

 

All are modified .. Of course, the image size may not be suitable, so we will try the demo first.

 

Finally, I would like to summarize that most of the controls we have developed are provided by Android, but it is inevitable that some modifications to the text size, style, image position, icon, and so on will be required to modify some places. These modifications are generally relatively small, but although they are if you want to make a small modification, Google may not give us a chance to rewrite it based on the gourd image, however, this takes a long time. We can also use the Java tool for reflection. This is simple and effective.

 

PS: I have to say that reflection is practical. It was often used in previous frameworks. It is also useful now, that is, geiliable.

 

 

 

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.