customized layout of SearchView

Dennis Lee
Aug 22, 2017 · 2 min read

Recently, I use android.support.v7.widget.SearchView in my android project to provide a searchable ui. But, the default layout was not what I expected. I try to search for any ways to setup UI properties.

Unfortunately, the SearchView doesn’t provide any direct APIs to set up the properties what I need. So I have to try another way to solve that.

By reading the source code, I found an attribute named layout:

When the SearchView was created, it attempts to obtain a layout resouce as the root view. If the resource was not found, the default resource abc_search_view will be used. Cool, this indicates we can customize a layout xml and style the SearchView with it by adding attribute just like below:

<style name="SearView" parent="Widget.AppCompat.SearchView">
<item name="layout">@layout/your_customized_layout</item>
<!-- other attributes -->
</style>

Notice

in our customized layout, we should declare views with the ids below or we will get the NullPointerException:

  • search_button it’s an ImageView type, clicked will show the editor and hide itself.
  • search_edit_frame container of the content.
  • search_mag_icon it’s an ImageView type, indicates search button icon.
  • search_plate seach plate, by default there are editor and close button inside.
  • search_src_text the editor, you can type some words on, it’s type of SearchView.SearchAutoComplete
  • search_close_button it’s type of ImageView, its clicked event will clear the focus of the editor and hide the search plate by default.
  • submit_area container of submit area.
  • search_go_btn go/commit button, it’s ImageView type.
  • search_voice_btn voice search button, it’s ImageView type.

If you just wanna change some basic appearances, you can follow this attributes of SearchView.

If you want to learn more, you can read the source code.

I’m new of SearchView usage, with any questions you can discuss with me.

)

Dennis Lee

Written by

An Android Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade