Android how to customize SearchView

In the previous page, I showed you how to display a searchview widget on a toolbar from the support library. In this tip, I am going to show you how to customize the SearchView. You learn how to change text color of the search text box, colors and images of search, submit, and close icons.

Change text color of the search text box
The first thing to do is creating a reference to the search text box using the following code.

EditText searchBox=((EditText) searchView.findViewById (android.support.v7.appcompat.R.id.search_src_text));
If you do not use the support library, use this code instead.
int searchTextId = getResources().getIdentifier ("android:id/search_src_text", null, null);
EditText searchBox=((EditText) searchView.findViewById (searchTextId));

Change color and image of the search button
Similarly, before you can change the color and image of the search button, you need to create a reference to search button.
ImageView searchButton = (ImageView) searchView.findViewById (android.support.v7.appcompat.R.id.search_button);
// change color
searchButton.setColorFilter (Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
// change image
searchButton.setImageResource(R.drawable.yourImage);

Change color and image of the search close icon
Changing the color and image of the search submit icon is done in the same way as that of the search button.
ImageView searchClose = (ImageView) searchView.findViewById (android.support.v7.appcompat.R.id.search_close_btn);
//change color
searchClose.setColorFilter (Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
// change image searchClose.setImageResource(R.drawable.yourImage);
Change color and image of the search submit icon
ImageView searchSubmit = (ImageView) searchView.findViewById (android.support.v7.appcompat.R.id.search_go_btn);
// change color
searchSubmit.setColorFilter (Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
// change image searchSubmit.setImageResource(R.drawable.yourImage);

custom searchview in android

Posted by: Dara | post date: 05-21-2016 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.