Android-----Change Icon original color and search box

Source: Internet
Author: User
Tags gettext

This blog is mainly about the following two points of knowledge

icon Change color :drawable, so that Android can also have iOS so convenient image tone conversion, like the same icon, but there are many places to use, and the color is different, you can use this method.

Search Box: General is edittext realization, this article realizes TextView picture and text Center, the keyboard searches.

Take a look at:

icon Change color: The first interface to the left (two-dimensional code) and the right (more) two implementations, I put in the picture is black, showing that it is white.

Search box: The first interface of the picture and Text center, you can also set the spacing, the second meet search set the keyboard search button, click the Search listener events, clear the contents of the icon.

Search Box layout:

<!--Search icon Settings left android:drawableleft= "@mipmap/icon_search" android:drawablepadding= "5DP"            icon and text spacing to the right android:drawableright= "@mipmap/round_close" android:paddingright= "8DP" Android:imeoptions= "Actionsearch" set to search button -       <EditTextAndroid:id= "@+id/search_text"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "30DP"Android:hint= "Enter the item to search"Android:background= "@drawable/search_gray"Android:layout_margintop= "10DP"Android:layout_marginleft= "9DP"android:textsize= "12SP"Android:drawableleft= "@mipmap/icon_search"Android:paddingleft= "9DP"android:drawablepadding= "5DP"Android:drawableright= "@mipmap/round_close"Android:paddingright= "8DP"android:imeoptions= "Actionsearch"Android:maxlines= "1"Android:singleline= "true"          />              

Keyboard monitoring:

Searchtext.setoneditoractionlistener (NewTextview.oneditoractionlistener () {@Override Public BooleanOneditoraction (TextView V,intActionId, KeyEvent event) {                if((ActionId = = 0 | | actionid = = 3) && Event! =NULL) {
             // prompt search content Toast.maketext (searchactivity. This, Searchtext.gettext (). toString (), Toast.length_long). Show (); //can jump to search page /*Intent intent= New Intent (Searchactivity.this,searchwebviewactivity.class); Intent.putextra ("model", model); Intent.putextra ("Search", Searchtext.gettext (). toString ()); StartActivity (Intent); Finish ();*/ } return false; } });

Home layout:

<LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"Android:minheight= "45DP"android:orientation= "Horizontal"android:gravity= "Center_vertical"        >        <ImageButtonAndroid:id= "@+id/home_left_scan"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:paddingright= "19DP"Android:paddingtop= "3DP"Android:paddingbottom= "3DP"Android:paddingleft= "11DP"android:layout_centervertical= "true"Android:background= "#00000000"            />        <com. Zhangqie.searchbox.view.DrawableTextView Android:id= "@+id/home_search"Android:layout_width= "Match_parent"Android:layout_height= "28DP"Android:layout_weight= "1"Android:background= "@drawable/search_view_background"android:gravity= "Center_vertical"Android:maxlines= "1"Android:text= "Enter search related content"Android:drawableleft= "@mipmap/icon_search"android:textsize= "12SP"android:drawablepadding= "11DP"            />        <ImageButtonAndroid:id= "@+id/home_right_more"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:layout_alignparentright= "true"Android:paddingright= "15DP"Android:paddingtop= "3DP"Android:paddingbottom= "3DP"Android:paddingleft= "15DP"Android:background= "#00000000"            />    </LinearLayout>

Custom Drawabletextview: (center text icon)

 Public classDrawabletextviewextendsTextView { PublicDrawabletextview (Context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); }     PublicDrawabletextview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicDrawabletextview (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {drawable[] drawables=Getcompounddrawables (); //get the Drawable object set by Drawableleftdrawable leftdrawable = drawables[0]; if(Leftdrawable! =NULL) {            //get the width of the leftdrawable            intLeftdrawablewidth =leftdrawable.getintrinsicwidth (); //get the spacing between drawable and text            intDrawablepadding =getcompounddrawablepadding (); //get the width of the text            intTextWidth = (int) Getpaint (). Measuretext (GetText (). toString (). Trim ()); intBodywidth = leftdrawablewidth + drawablepadding +TextWidth;            Canvas.save (); Canvas.translate ((getwidth ()-Bodywidth)/2, 0); }        Super. OnDraw (canvas); }}

Seemingly simple effect, in fact, is not simple; come on! Have a problem can sweep avatar plus newly created group @ i

SOURCE download

Android-----Change Icon original color and search box

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.