Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/MiguelCatalan/MaterialSearchView

Cute library to implement SearchView in a Material Design Approach
https://github.com/MiguelCatalan/MaterialSearchView

Last synced: 1 day ago
JSON representation

Cute library to implement SearchView in a Material Design Approach

Lists

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

#Native version
Maybe it would be useful to take a look into the new official approach
http://www.materialdoc.com/search-filter/

# 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

<!-- Background for the search bar -->
<item name="searchBackground">@color/theme_primary</item>

<!-- Change voice icon -->
<item name="searchVoiceIcon">@drawable/ic_action_voice_search_inverted</item>

<!-- Change clear text icon -->
<item name="searchCloseIcon">@drawable/ic_action_navigation_close_inverted</item>

<!-- Change up icon -->
<item name="searchBackIcon">@drawable/ic_action_navigation_arrow_back_inverted</item>

<!-- Change icon for the suggestions -->
<item name="searchSuggestionIcon">@drawable/ic_suggestion</item>

<!-- Change background for the suggestions list view -->
<item name="searchSuggestionBackground">@android:color/white</item>

<!-- Change text color for edit text. This will also be the color of the cursor -->
<item name="android:textColor">@color/theme_primary_text_inverted</item>

<!-- Change hint text color for edit text -->
<item name="android:textColorHint">@color/theme_secondary_text_inverted</item>

<!-- Hint for edit text -->
<item name="android:hint">@string/search_hint</item>

```
#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.