# MaterialSearchView **Repository Path**: android_locker/MaterialSearchView ## Basic Information - **Project Name**: MaterialSearchView - **Description**: https://github.com/MiguelCatalan/MaterialSearchView.git - **Primary Language**: Android - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-11-22 - **Last Updated**: 2021-11-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MaterialSearchView Cute library to implement SearchView in a Material Design Approach. *Works from Android API 14 (ICS) and above*. ![sample](https://raw.githubusercontent.com/MiguelCatalan/MaterialSearchView/master/art/voice.gif) ![sample](https://raw.githubusercontent.com/MiguelCatalan/MaterialSearchView/master/art/default.gif) Get it on Google Play # Usage **Add the dependencies to your gradle file:** ```javascript dependencies { compile 'com.miguelcatalan:materialsearchview:1.4.0' } ``` **Add MaterialSearchView to your layout file along with the Toolbar** *(Add this block at the bottom of your layout, in order to display it over the rest of the view)*: ```xml ``` **Add the search item into the menu file:** ```xml ``` **Add define it in the *onCreateOptionsMenu*:** ```java @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); MenuItem item = menu.findItem(R.id.action_search); searchView.setMenuItem(item); return true; } ``` **Set the listeners:** ```java MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view); searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { //Do some magic return false; } @Override public boolean onQueryTextChange(String newText) { //Do some magic return false; } }); searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() { @Override public void onSearchViewShown() { //Do some magic } @Override public void onSearchViewClosed() { //Do some magic } }); ``` # Use VoiceSearch **Allow/Disable it in the code:** ```java searchView.setVoiceSearch(true); //or false ``` **Handle the response:** ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) { ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); if (matches != null && matches.size() > 0) { String searchWrd = matches.get(0); if (!TextUtils.isEmpty(searchWrd)) { searchView.setQuery(searchWrd, false); } } return; } super.onActivityResult(requestCode, resultCode, data); } ``` # Add suggestions **Define them in the resources as a *string-array*:** ```xml Android iOS SCALA Ruby JavaScript ``` **Add them to the object:** ```java searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions)); ``` # Style it! ```xml ``` #Custom cursor **Create the drawable:** ```xml ``` **And add it to the object:** ```java searchView.setCursorDrawable(R.drawable.custom_cursor); ``` # Using AppBarLayout? It is a little bit tricky but can be achieved using this: ```xml ``` # Bonus **Close on backpressed:** ```java @Override public void onBackPressed() { if (searchView.isSearchOpen()) { searchView.closeSearch(); } else { super.onBackPressed(); } } ``` # Help me Pull requests are more than welcome, help me and others improve this awesome library. The code is based in the Krishnakapil original concept. # License Copyright 2015 Miguel Catalan Bañuls 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.