Android 3rd party library to make a MaterialSearchBar in a easy mode. This beautiful and easy to use library will help to add a Material Design SearchView in your project.
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.simonebortolin:MaterialSearchBar:1.2.2'
}
then add SearchBar to your activity:
<com.mancj.materialsearchbar.MaterialSearchBar
style="@style/MaterialSearchBarLight"
app:mt_speechMode="true"
app:mt_hint="Custom hint"
app:mt_maxSuggestionsCount="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchBar" />
Attribute | Description |
---|---|
mt_speechMode | if set to true, microphone icon will be displayed instead of search icon |
mt_maxSuggestionsCount | specifies the max number of search queries stored |
mt_navIconEnabled | set navigation icon enabled |
mt_roundedSearchBarEnabled | use capsule shaped searchbar on v21+ and revert to default on lower |
mt_dividerColor | set the colors of the suggestion and menu dividers |
mt_searchBarColor | set the main color of the searchbar |
mt_menuIconDrawable | set drawable of the menu icon |
mt_searchIconDrawable | set drawable of the search icon when speech mode is false |
mt_speechIconDrawable | set drawable of the speech icon when speech mode is true |
mt_backIconDrawable | set drawable of the back arrow icon |
mt_clearIconDrawable | set drawable of the clear icon |
mt_navIconDrawable | set drawable of the nav icon |
mt_navIconTint | set tint color of nav/back animated icon |
mt_menuIconTint | set tint color of the menu icon |
mt_searchIconTint | set tint color search/speech icon |
mt_backIconTint | set tint color of the back arrow icon |
mt_clearIconTint | set tint color of the clear icon |
mt_navIconUseTint | should the animated nav icon use tint color |
mt_menuIconUseTint | should the menu icon use the tint color |
mt_searchIconUseTint | should the search/speech icon use the tint color |
mt_backIconUseTint | should the back icon use the tint color |
mt_clearIconUseTint | should the clear icon use the tint color |
mt_hint | set the text of the hint when the searchbar is focused and search query is empty |
mt_placeholder | set the placeholder text when the MaterialSearchBar is not focused |
mt_textColor | set text color |
mt_hintColor | set hint color |
mt_placeholderColor | set placeholder color |
mt_textCursorTint | set text cursors tint |
mt_leftTextSelectorDrawable | set left text selector drawable |
mt_middleTextSelectorDrawable | set middle text selector drawable |
mt_rightTextSelectorDrawable | set right text selector drawable |
mt_leftTextSelectorTint | set left text selector tint color |
mt_middleTextSelectorTint | set middle text selector tint color |
mt_rightTextSelectorTint | set right text selector tint color |
mt_handlesTintEnabled | should text selectors use tint color |
mt_highlightedTextColor | set the text highlight tint color |
mt_suggestionsAnimationSpeed | set the animation speed |
Inflate menu for searchBar
- Parameters:
menuResource
— - menu resource
Inflate menu for searchBar with custom Icon
- Parameters:
menuResource
— - menu resourceicon
— - icon resource id
Get popup menu
- Returns: PopupMenu
Capsule shaped searchbar enabled Only works on SDK V21+ due to odd behavior on lower
Setup editText coloring and drawables
Register listener for search bar callbacks.
- Parameters:
onSearchActionListener
— the callback listener
Hides search input and close arrow
Shows search input and close arrow
Suggestion 목록을 보여줄 때, 애니메이션을 설정한다.
Check if suggestions are shown
- Returns: return result
Check if suggestions are enabled
Set suggestions enabled
Set Menu Icon Drawable
- Parameters:
menuIconResId
— icon resource id
Set search icon drawable
- Parameters:
searchIconResId
— icon resource id
Set back arrow icon drawable
- Parameters:
arrowIconResId
— icon resource id
Set clear icon drawable
- Parameters:
clearIconResId
— icon resource id
Set the nav icon drawable
- Parameters:
navIconResId
— icon resource id
Set the animated nav icon drawable
Set the tint color of the navigation icon
- Parameters:
navIconTint
— nav icon color
Set the tint color of the menu icon
- Parameters:
menuIconTint
— menu icon color
Set the tint color of the search/speech icon
- Parameters:
searchIconTint
— search icon color
Set the tint color of the back arrow icon
- Parameters:
arrowIconTint
— arrow icon color
Set the tint color of the clear icon
- Parameters:
clearIconTint
— clear icon tint
Show a borderless ripple(circular) when icon is pressed Borderless only available on SDK V21+
- Parameters:
borderlessRippleEnabled
— true for borderless, false for default
Sets search bar hintText
- Parameters:
hintText
— hintText text
Set the place holder text
- Parameters:
placeholder
— placeholder text
sets the speechMode for the search bar. If set to true, microphone icon will display instead of the search icon. Also clicking on this icon will trigger the callback method onButtonClicked()
- Parameters:
speechMode
— enable speech - See also:
- #BUTTON_SPEECH
- OnSearchActionListener#onButtonClicked(int)
True if MaterialSearchBar is in speech mode
- Returns: speech mode
Check if search bar is in edit mode
- Returns: true if search bar is in edit mode
Specifies the maximum number of search queries stored until the activity is destroyed
- Parameters:
maxSuggestionsCount
— maximum queries
Sets a custom adapter for suggestions list view.
- Parameters:
suggestionAdapter
— customized adapter
Returns the last search queries. The queries are stored only for the duration of one activity session. When the activity is destroyed, the queries will be deleted. To save queries, use the method getLastSuggestions(). To recover the queries use the method setLastSuggestions().
List< String > will be returned if You don't use custom adapter.
- Returns: array with the latest search queries
- See also:
- #setLastSuggestions(List)
- #setMaxSuggestionCount(int)
Sets the array of recent search queries. It is advisable to save the queries when the activity is destroyed and call this method when creating the activity.
Pass a List< String > if You don't use custom adapter.
- Parameters:
suggestions
— an array of queries - See also:
- #getLastSuggestions()
- #setMaxSuggestionCount(int)
Changes the array of recent search queries with animation.
Pass a List< String > if You don't use custom adapter.
- Parameters:
suggestions
— an array of queries
Allows you to intercept the suggestions click event
This method will not work with custom Suggestion Adapter
- Parameters:
listener
— click listener
Set search input text color
- Parameters:
textColor
— text color
Set text input hintText color
- Parameters:
hintColor
— text hintText color
Set placeholder text color
- Parameters:
placeholderColor
— placeholder color
Set the color of the highlight when text is selected
- Parameters:
highlightedTextColor
— selected text highlight color
Set navigation drawer menu icon enabled
- Parameters:
navButtonEnabled
— icon enabled
Set navigation up menu enabled. Can display back icon (up navigation icon) instead of menu button {@link #setNavButtonEnabled(boolean)}.
- Parameters:
upButtonEnabled
— icon enabled
Enable capsule shaped SearchBar (API 21+)
- Parameters:
roundedSearchBarEnabled
— capsule shape enabled
Set CardView elevation
- Parameters:
elevation
— desired elevation
Get search text
- Returns: text
Set search text
- Parameters:
text
— text
Add text watcher to searchbar's EditText
- Parameters:
textWatcher
— textWatcher to add
Remove text watcher to searchbar's EditText
- Parameters:
textWatcher
— textWatcher to add
For calculate the height change when item delete or add animation false is return the full height of item, true is return the height of position subtraction one
- Parameters:
isSubtraction
— is subtraction enabled
Interface definition for MaterialSearchBar callbacks.
Invoked when SearchBar opened or closed
- Parameters:
enabled
— state
Invoked when search confirmed and "search" button is clicked on the soft keyboard
- Parameters:
text
— search input
Invoked when "speech" or "navigation" buttons clicked.
- Parameters:
buttonCode
— {@link #BUTTON_NAVIGATION}, {@link #BUTTON_SPEECH} or {@link #BUTTON_BACK} will be passed
Here is a simple example of using MaterialSearchBar
Kotlin
searchBar.inflateMenu(R.menu.main)
searchBar.text = "Hello World!"
searchBar.setCardViewElevation(10)
searchBar.setUpButtonEnabled(false)
searchBar.addTextChangeListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun afterTextChanged(editable: Editable) {
}
})
searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
override fun onSearchStateChanged(enabled: Boolean) {
}
override fun onSearchConfirmed(text: CharSequence) {
}
override fun onButtonClicked(buttonCode: Int) {
when (buttonCode) {
MaterialSearchBar.BUTTON_NAVIGATION -> {
}
MaterialSearchBar.BUTTON_SPEECH -> {
}
MaterialSearchBar.BUTTON_BACK -> searchBar.closeSearch()
}
}
})
Java
searchBar = findViewById(R.id.searchBar);
searchBar.inflateMenu(R.menu.main);
searchBar.setText("Hello World!");
searchBar.setCardViewElevation(10);
searchBar.setUpButtonEnabled(false);
searchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
searchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
}
@Override
public void onButtonClicked(int buttonCode) {
switch (buttonCode) {
case MaterialSearchBar.BUTTON_NAVIGATION:
break;
case MaterialSearchBar.BUTTON_SPEECH:
break;
case MaterialSearchBar.BUTTON_BACK:
searchBar.closeSearch();
break;
}
}
});
However, the library is very customizable, and you can easily change the view of the adapter.
I thank all the authors of the various commits that I have included in my fork
The MIT License (MIT)
Copyright (c) 2016 mancj
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Please let me know if you use the library in your applications.
I want to collect and publish this list.