项目作者: mancj

项目描述 :
Material Design Search Bar for Android
高级语言: Java
项目地址: git://github.com/mancj/MaterialSearchBar.git
创建时间: 2016-07-20T13:02:53Z
项目社区:https://github.com/mancj/MaterialSearchBar

开源协议:Other

下载


Material SearchBar Android

Material Design Search Bar for Android
Android Arsenal


This beautiful and easy to use library will help to add Lollipop Material Design SearchView in your project.







See our Wiki

How to use

to include SearchBar to your project:

add this code to the the project level build.gradle file

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }

add the dependency to the the app level build.gradle file

  1. dependencies {
  2. implementation 'com.github.mancj:MaterialSearchBar:X.X.X'
  3. }

then add SearchBar to your activity:

  1. <com.mancj.materialsearchbar.MaterialSearchBar
  2. style="@style/MaterialSearchBarLight"
  3. app:mt_speechMode="true"
  4. app:mt_hint="Custom hint"
  5. app:mt_maxSuggestionsCount="10"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/searchBar" ></com.mancj.materialsearchbar.MaterialSearchBar>

MaterialSearchBar has the following xml attributes:

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_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_highlightedTextColor set the text highlight tint color

public methods:

  • addTextChangeListener(TextWatcher textWatcher)
  • clearSuggestions()
  • closeSearch()
  • openSearch()
  • getLastSuggestions()
  • getMenu()
  • getText()
  • hideSuggestionList()
  • inflateMenu(int menuResource)
  • inflateMenu(int menuResource, int icon)
  • isSearchOpened()
  • isSpeechModeEnabled()
  • isSuggestionsVisible()
  • setArrowIcon(int arrowIconResId)
  • setArrowIconTint(int arrowIconTint)
  • setCardViewElevation(int elevation)
  • setClearIcon(int clearIconResId)
  • setClearIconTint(int clearIconTint)
  • setCustomSuggestionAdapter(SuggestionsAdapter suggestionAdapter)
  • setDividerColor(int dividerColor)
  • setHint(CharSequence hintText)
  • setIconRippleStyle(boolean borderlessRippleEnabled)
  • setLastSuggestions(List suggestions)
  • setMaxSuggestionCount(int maxSuggestionsCount)
  • setMenuDividerEnabled(boolean menuDividerEnabled)
  • setMenuIcon(int menuIconResId)
  • setMenuIconTint(int menuIconTint)
  • setNavButtonEnabled(boolean navButtonEnabled)
  • setNavIconTint(int navIconTint)
  • setOnSearchActionListener(OnSearchActionListener onSearchActionListener)
  • setPlaceHolder(CharSequence placeholder)
  • setPlaceHolderColor(int placeholderColor)
  • setRoundedSearchBarEnabled(boolean roundedSearchBarEnabled)
  • setSearchIcon(int searchIconResId)
  • setSearchIconTint(int searchIconTint)
  • setSpeechModeEnabled(boolean speechMode)
  • setSuggestionsClickListener(SuggestionsAdapter.OnItemViewClickListener listener)
  • setText(String text)
  • setTextColor(int textColor)
  • setTextHighlightColor(int highlightedTextColor)
  • setTextHintColor(int hintColor)
  • showSuggestions()
  • updateLastSuggestions(List suggestions)

Styling Material SearchBar

Custom Style - styles.xml
Create a custom style and use one of the provided styles as the parent.

  1. Provided Styles are: MaterialSearchBarLight and MaterialSearchBarDark
  2. Example:
  3. <style name="MyCustomTheme" parent="MaterialSearchBarLight">
  4. <item name="mt_searchBarColor">@color/searchBarPrimaryColor</item>
  5. <item name="mt_dividerColor">@color/searchBarDividerColor</item>
  6. <item name="mt_navIconTint">@color/searchBarNavIconTintColor</item>
  7. <item name="mt_searchIconTint">@color/searchBarSearchIconTintColor</item>
  8. <item name="mt_clearIconTint">@color/searchBarClearIconTintColor</item>
  9. <item name="mt_menuIconTint">@color/searchBarMenuIconTintColor</item>
  10. <item name="mt_backIconTint">@color/searchBarBackIconTintColor</item>
  11. <item name="mt_textCursorTint">@color/searchBarCursorColor</item>
  12. <item name="mt_textColor">@color/searchBarTextColor</item>
  13. <item name="mt_hintColor">@color/searchBarHintColor</item>
  14. <item name="mt_placeholderColor">@color/searchBarPlaceholderColor</item>
  15. <item name="mt_highlightedTextColor">@color/searchBarTextHighlightColor</item>
  16. </style>

OR

Custom Colors - colors.xml
Simply set/change these colors(or some) and you have your custom style.

  1. //Material SearchBar Light Theme Colors
  2. <color name="searchBarIconColor">#3a3a3a</color>
  3. //Base
  4. <color name="searchBarPrimaryColor">#FFFFFF</color>
  5. <color name="searchBarCursorColor">#8000a1ff</color>
  6. <color name="searchBarDividerColor">#1F000000</color>
  7. //Icons
  8. <color name="searchBarNavIconTintColor">@color/searchBarIconColor</color>
  9. <color name="searchBarMenuIconTintColor">@color/searchBarIconColor</color>
  10. <color name="searchBarSearchIconTintColor">@color/searchBarIconColor</color>
  11. <color name="searchBarClearIconTintColor">@color/searchBarIconColor</color>
  12. <color name="searchBarBackIconTintColor">@color/searchBarIconColor</color>
  13. //Text
  14. <color name="searchBarTextColor">#DE000000</color>
  15. <color name="searchBarHintColor">#42000000</color>
  16. <color name="searchBarPlaceholderColor">#8A000000</color>
  17. <color name="searchBarTextHighlightColor">#8000a1ff</color>
  18. //Base
  19. <color name="searchBarPrimaryColorDark">#303030</color>
  20. <color name="searchBarDividerColorDark">#1FFFFFFF</color>
  21. //Material SearchBar Dark Theme Colors
  22. <color name="searchBarIconColorDark">#00a1ff</color>
  23. //Icons
  24. <color name="searchBarNavIconTintColorDark">@color/searchBarIconColorDark</color>
  25. <color name="searchBarMenuIconTintColorDark">@color/searchBarIconColorDark</color>
  26. <color name="searchBarSearchIconTintColorDark">@color/searchBarIconColorDark</color>
  27. <color name="searchBarClearIconTintColorDark">@color/searchBarIconColorDark</color>
  28. <color name="searchBarBackIconTintColorDark">@color/searchBarIconColorDark</color>
  29. //Text
  30. <color name="searchBarTextColorDark">#DEFFFFFF</color>
  31. <color name="searchBarHintColorDark">#42FFFFFF</color>
  32. <color name="searchBarPlaceholderColorDark">#8AFFFFFF</color>
  33. <color name="searchBarTextHighlightColorDark">#BF00a1ff</color>

To save search queries when the activity is destroyed, use the method searchBar.getLastSuggestions() and then, to restore them use searchBar.setLastSuggestions(List<String>); as shown in the example below

Example

Here is a simple example of using MaterialSearchBar

  1. private List<String> lastSearches;
  2. private MaterialSearchBar searchBar;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
  8. searchBar.setHint("Custom hint");
  9. searchBar.setSpeechMode(true);
  10. //enable searchbar callbacks
  11. searchBar.setOnSearchActionListener(this);
  12. //restore last queries from disk
  13. lastSearches = loadSearchSuggestionFromDisk();
  14. searchBar.setLastSuggestions(list);
  15. //Inflate menu and setup OnMenuItemClickListener
  16. searchBar.inflateMenu(R.menu.main);
  17. searchBar.getMenu().setOnMenuItemClickListener(this);
  18. }
  19. @Override
  20. protected void onDestroy() {
  21. super.onDestroy();
  22. //save last queries to disk
  23. saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
  24. }
  25. @Override
  26. public void onSearchStateChanged(boolean enabled) {
  27. String s = enabled ? "enabled" : "disabled";
  28. Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
  29. }
  30. @Override
  31. public void onSearchConfirmed(CharSequence text) {
  32. startSearch(text.toString(), true, null, true);
  33. }
  34. @Override
  35. public void onButtonClicked(int buttonCode) {
  36. switch (buttonCode){
  37. case MaterialSearchBar.BUTTON_NAVIGATION:
  38. drawer.openDrawer(Gravity.LEFT);
  39. break;
  40. case MaterialSearchBar.BUTTON_SPEECH:
  41. openVoiceRecognizer();
  42. }
  43. }

More Examples