Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion '25.0.0'

defaultConfig {
minSdkVersion 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void setAdapter(ListAdapter adapter) {
public void setSuggestions(String[] suggestions) {
if (suggestions != null && suggestions.length > 0) {
mTintView.setVisibility(VISIBLE);
final SearchAdapter adapter = new SearchAdapter(mContext, suggestions, suggestionIcon, ellipsize);
final SearchAdapter adapter = new SearchAdapter(mContext, suggestions, suggestionIcon, ellipsize, false);
setAdapter(adapter);

setOnItemClickListener(new AdapterView.OnItemClickListener() {
Expand All @@ -429,6 +429,25 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
}

public void setSuggestions(String[] suggestions, boolean checkSuggestion) {

if (suggestions != null && suggestions.length > 0) {
mTintView.setVisibility(VISIBLE);
final SearchAdapter adapter = new SearchAdapter(mContext, suggestions, suggestionIcon, ellipsize, checkSuggestion);
setAdapter(adapter);

setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setQuery((String) adapter.getItem(position), submit);
}
});
} else {
mTintView.setVisibility(GONE);
}

}

/**
* Dismiss the suggestions list.
*/
Expand Down Expand Up @@ -668,70 +687,70 @@ public void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(mSavedState.getSuperState());
}

static class SavedState extends BaseSavedState {
String query;
boolean isSearchOpen;
static class SavedState extends BaseSavedState {
String query;
boolean isSearchOpen;

SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
this.query = in.readString();
this.isSearchOpen = in.readInt() == 1;
}
SavedState(Parcelable superState) {
super(superState);
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(query);
out.writeInt(isSearchOpen ? 1 : 0);
}
private SavedState(Parcel in) {
super(in);
this.query = in.readString();
this.isSearchOpen = in.readInt() == 1;
}

//required field that makes Parcelables from a Parcel
public static final Creator<SavedState> CREATOR =
new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

public interface OnQueryTextListener {

/**
* Called when the user submits the query. This could be due to a key press on the
* keyboard or due to pressing a submit button.
* The listener can override the standard behavior by returning true
* to indicate that it has handled the submit request. Otherwise return false to
* let the SearchView handle the submission by launching any associated intent.
*
* @param query the query text that is to be submitted
* @return true if the query has been handled by the listener, false to let the
* SearchView perform the default action.
*/
boolean onQueryTextSubmit(String query);

/**
* Called when the query text is changed by the user.
*
* @param newText the new content of the query text field.
* @return false if the SearchView should perform the default action of showing any
* suggestions if available, true if the action was handled by the listener.
*/
boolean onQueryTextChange(String newText);
}

public interface SearchViewListener {
void onSearchViewShown();

void onSearchViewClosed();
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeString(query);
out.writeInt(isSearchOpen ? 1 : 0);
}

//required field that makes Parcelables from a Parcel
public static final Creator<SavedState> CREATOR =
new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

public interface OnQueryTextListener {

/**
* Called when the user submits the query. This could be due to a key press on the
* keyboard or due to pressing a submit button.
* The listener can override the standard behavior by returning true
* to indicate that it has handled the submit request. Otherwise return false to
* let the SearchView handle the submission by launching any associated intent.
*
* @param query the query text that is to be submitted
* @return true if the query has been handled by the listener, false to let the
* SearchView perform the default action.
*/
boolean onQueryTextSubmit(String query);

/**
* Called when the query text is changed by the user.
*
* @param newText the new content of the query text field.
* @return false if the SearchView should perform the default action of showing any
* suggestions if available, true if the action was handled by the listener.
*/
boolean onQueryTextChange(String newText);
}

public interface SearchViewListener {
void onSearchViewShown();

void onSearchViewClosed();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private Drawable suggestionIcon;
private LayoutInflater inflater;
private boolean ellipsize;
private boolean checkWholeString;

public SearchAdapter(Context context, String[] suggestions) {
inflater = LayoutInflater.from(context);
data = new ArrayList<>();
this.suggestions = suggestions;
}

public SearchAdapter(Context context, String[] suggestions, Drawable suggestionIcon, boolean ellipsize) {
public SearchAdapter(Context context, String[] suggestions, Drawable suggestionIcon, boolean ellipsize, boolean checkWholeString) {
inflater = LayoutInflater.from(context);
data = new ArrayList<>();
this.suggestions = suggestions;
this.suggestionIcon = suggestionIcon;
this.ellipsize = ellipsize;
this.checkWholeString = checkWholeString;
}



@Override
public Filter getFilter() {
Filter filter = new Filter() {
Expand All @@ -54,8 +58,14 @@ protected FilterResults performFiltering(CharSequence constraint) {
List<String> searchData = new ArrayList<>();

for (String string : suggestions) {
if (string.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
searchData.add(string);
if(checkWholeString){
if(string.toLowerCase().contains(constraint.toString().toLowerCase())){
searchData.add(string);
}
} else {
if (string.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
searchData.add(string);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "com.miguelcatalan.materialsearchview.sample"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
searchView.setVoiceSearch(false);
searchView.setCursorDrawable(R.drawable.custom_cursor);
searchView.setEllipsize(true);
searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));
searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions), true);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Expand Down