Floating Search View
arimorty/FloatingSearchView
Complete Kotlin/AndroidX port ofAn implementation of a floating search box with search suggestions, also called persistent search bar.
...
Usage
- In your dependencies, add
implementation 'xyz.quaver:floatingsearchview:1.2.0-rc2'
- Add a FloatingSearchView to your view hierarchy, and make sure that it takes up the full width and height of the screen
- Listen to query changes and provide suggestion items that implement SearchSuggestion
Example:
<xyz.quaver.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:searchBarMarginLeft="@dimen/search_view_inset"
app:searchBarMarginTop="@dimen/search_view_inset"
app:searchBarMarginRight="@dimen/search_view_inset"
app:searchHint="Search..."
app:suggestionAnimDuration="250"
app:showSearchKey="false"
app:leftActionMode="showHamburger"
app:menu="@menu/menu_main"
app:elevation="4dp"
app:close_search_on_keyboard_dismiss="true"/>
Kotlin
binding.searchView.onQueryChangeListener = { oldQuery, newQuery -> {
//get suggestions based on newQuery
//pass them on to the search view
binding.searchView.swapSuggestions(newSuggestions);
}
Java
mSearchView.setOnQueryChangeListener((oldQuery, newQuery) -> {
//get suggestions based on newQuery
//pass them on to the search view
mSearchView.swapSuggestions(newSuggestions);
return Unit.INSTANCE;
});
Left action mode:
The left action can be configured as follows:
Add
app:leftActionMode="[insert one of the options from table below]"
showHamburger | ![]() |
showSearch | ![]() |
showHome | ![]() |
noLeftAction | ![]() |
Listen to hamburger button clicks:
binding.searchView.onMenuClickListener = object: FloatingSearchView.OnLeftMenuClickListener {
...
}
To quickly connect your NavigationDrawer to the hamburger button:
binding.menuView.attachNavigationDrawerToMenuButton(mDrawerLayout);
Listen to home (back arrow) button clicks:
binding.menuView.setOnHomeActionClickListener(
new FloatingSearchView.OnHomeActionClickListener() { ... });
Configure menu items:
Add a menu resource
app:menu="@menu/menu_main"
In the menu resource, set items' app:showAsAction="[insert one of the options described in the table below]"
never | Puts the menu item in the overflow options popup |
ifRoom | Shows an action icon for the menu if the following conditions are met: 1. The search is not focused. 2. There is enough room for it. |
always | Shows an action icon for the menu if there is room, regardless of whether the search is focused or not. |
Listen for item selections
binding.searchView.onMenuItemClickListener = { item ->
}
Configure suggestion item:
First, implement SearchSuggestion
Optional:
Set a callback for when a given suggestion is bound to the suggestion list.
For the history icons to show, you would need to implement this. Refer to the sample app for an example implementation.
binding.searchView.onBindSuggestionCallback = { binding, item, itemPosition ->
// suggestionView = binding.root, leftIcon = binding.leftIcon, textView = binding.body
//here you can set some attributes for the suggestion's left icon and text. For example,
//you can choose your favorite image-loading library for setting the left icon's image.
});
Styling:
Available styling:
<style name="SearchView">
<item name="backgroundColor"></item>
<item name="viewSearchInputTextColor"></item>
<item name="viewSuggestionItemTextColor"></item>
<item name="hintTextColor"></item>
<item name="dividerColor"></item>
<item name="clearBtnColor"></item>
<item name="leftActionColor"></item>
<item name="menuItemIconColor"></item>
<item name="suggestionRightIconColor"></item>
<item name="actionMenuOverflowColor"></item>
</style>
backgroundColor
uses colorSurface
value defined in Theme.
To change backgroundColor
according to Light/Dark Theme, define colorSurface
or use Theme.Material
/Theme.MaterialComponents
as a parent.
Otherwise, backgroundColor
defaults to White.
License
tom5079/FloatingSearchView was ported from arimorty/FloatingSearchView
Copyright 2015 Ari C.
Copyright 2020 tom5079
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.