MaterialSearchView


Source link: https://github.com/Mauker1/MaterialSearchView

MaterialSearchView

Android SearchView based on Material Design guidelines. The MaterialSearchView will overlay a Toolbar or ActionBar as well as display a ListView for the user to show suggested or recent searches.

Download

To add the MaterialSearchView library to your Android Studio project, simply add the following gradle dependency:

compile 'br.com.mauker.materialsearchview:materialsearchview:1.2.2'

This library is supported with a min SDK of 14.

Important note: If you're still using version 1.0.3, it's recommended to upgrade to the latest version as soon as possible. For more information, please see this issue.

Setup

Before you can use this lib, you have to implement a class named MsvAuthority inside the br.com.mauker package on your app module, and it should have a public static String variable called CONTENT_AUTHORITY. Give it the value you want and don't forget to add the same name on your manifest file. The lib will use this file to set the Content Provider authority.

Example:

MsvAuthority.java

package br.com.mauker;  public class MsvAuthority {

  public static final String CONTENT_AUTHORITY = "br.com.mauker.materialsearchview.searchhistorydatabase"; 
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest ...>

<application ... >

<provider

android:name="br.com.mauker.materialsearchview.db.HistoryProvider"

android:authorities="br.com.mauker.materialsearchview.searchhistorydatabase"

android:exported="false"

android:protectionLevel="signature"

android:syncable="true"/>
  </application>  </manifest>

Proguard note: Some of you might experience some problems with Proguard deleting the authority class, to solve those problems, add the following lines on your proguard file:

-keep class br.com.mauker.MsvAuthority -keepclassmembers class br.com.mauker.** {
 *; 
}
 

Usage

To open the search view on your app, add the following code to your layout:

<br.com.mauker.materialsearchview.MaterialSearchView
  android:id="@+id/search_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

Then, inside your Activity get the reference:

// Activity: MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);

To open the search view, simply call the searchView.openSearch() method.

To close the search view, call the searchView.closeSearch() method.

You can check if the view is open by using the searchView.isOpen() method.

As from Version 1.2.1 it's also possible to get the query anytime by using the searchView.getCurrentQuery() method.

Protip: To close the search view using the back button, put the following code on your Activity:

@Override public void onBackPressed() {

  if (searchView.isOpen()) {

// Close the search on the back button press.

searchView.closeSearch();

  
}
 else {

super.onBackPressed();

  
}
 
}

Search history and suggestions

You can provide search suggestions by using the following methods:

  • addSuggestions(String[] suggestions)
  • addSuggestions(ArrayList<String> suggestions)

It's also possible to add a single suggestion using the following method:

  • addSuggestion(String suggestion)

To remove all the search suggestions use:

  • clearSuggestions()

And to remove a single suggestion, use the following method:

  • removeSuggestion(String suggestion)

The search history is automatically handled by the view, and it can be cleared by using:

  • clearHistory()

You can also remove both by using the method below:

  • clearAll()

Modifying the suggestion list behavior

The suggestion list is based on a ListView, and as such you can define the behavior of the item click by using the MaterialSearchView#setOnItemClickListener() method.

If you want to submit the query from the selected suggestion, you can use the snippet below:

searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// Do something when the suggestion list is clicked.

String suggestion = searchView.getSuggestionAtPosition(position);

 searchView.setQuery(suggestion, true);

  
}
 
}
);

If you just want to set the text on the search view text field when the user selects the suggestion, change the second argument from the searchView#setQuery() from true to false.

Styling the View

You can change how your MaterialSearchView looks like. To achieve that effect, try to add the following lines to your styles.xml:

<style name="MaterialSearchViewStyle">
  <item name="searchBackground">@color/white_ish</item>
  <item name="searchVoiceIcon">@drawable/ic_action_voice_search</item>
  <item name="searchCloseIcon">@drawable/ic_action_navigation_close</item>
  <item name="searchBackIcon">@drawable/ic_action_navigation_arrow_back</item>
  <item name="searchSuggestionBackground">@color/search_layover_bg</item>
  <item name="searchBarHeight">?attr/actionBarSize</item>
  <item name="voiceHintPrompt">@string/hint_prompt</item>
  <item name="android:textColor">@color/black</item>
  <item name="android:textColorHint">@color/gray_50</item>
  <item name="android:hint">@string/search_hint</item>
  <item name="android:inputType">textCapWords</item> </style>

And add this line on your br.com.mauker.materialsearchview.MaterialSearchView tag:

style="@style/MaterialSearchViewStyle"

So it'll look like:

<br.com.mauker.materialsearchview.MaterialSearchView
  android:id="@+id/search_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  style="@style/MaterialSearchViewStyle"/>

Interfaces

Currently there are two interfaces that you can use to instantiate listeners for:

  • OnQueryTextListener: This interface handles either QueryTextChange or QueryTextSubmit events inside the MaterialSearchView.
  • SearchViewListener: This interfaces handles the open or close events of the MaterialSearchView.

Languages

The MaterialSearchView supports the following languages:

  • English (en_US);
  • Brazillian Portuguese (pt_BR);
  • Italian (Thanks to Francesco Donzello);
  • French (Thanks to Robin);
  • Bosnian, Croatian and Serbian (Thanks to Luke).

Sample GIF

More Info

For more use cases, and some examples, you can check the sample app.

Credits

This library was created by Maurício Pessoa with contributions from:

JCenter version was made possible with help from:

This project was inspired by the MaterialSearchView library by krishnakapil.

License

The MaterialSearchView library is available under the Apache 2.0 License.

Resources

Detects gestures on Android with listener and RxJava Observable.

AndRouter is a android framework used to map url to activities or actions.

APT processor to generate update methods for POJOs.

This project aims to provide pretty cool equalizer for any Android audio project.

This is the real android video player view with fullscreen function.

You can handle prefectures information more easily in Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes