Last active
September 28, 2019 07:44
-
-
Save hosseiniSeyRo/65d75e68f07902e186f720f26ae2870f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* configure Search */ | |
private void configureSearch() { | |
// add text change listener for searchView | |
searchView.addTextChangedListener(searchTextWatcher); | |
// set search Suggestion Adapter | |
searchSuggestionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line); | |
searchView.setAdapter(searchSuggestionAdapter); | |
searchView.setThreshold(2); | |
searchView.setOnItemClickListener((parent, view, position, id) -> { | |
//TODO get data for search keyword and show in UI | |
}); | |
// get suggestion data | |
getSuggestionData(); | |
// handle keyboard search btn click | |
searchView.setOnEditorActionListener((v, actionId, event) -> { | |
if (actionId == EditorInfo.IME_ACTION_SEARCH) { | |
// disappear suggestions box | |
searchView.dismissDropDown(); | |
// close keyboard | |
closeKeyboard(); | |
// save search keyword in db | |
saveSearchKeywordInDb(searchKeyword); | |
// get search keyword | |
String searchKeyword = v.getText().toString().trim(); | |
// TODO get data for search keyword | |
return true; | |
} | |
return false; | |
}); | |
} | |
/* search text watcher*/ | |
private TextWatcher searchTextWatcher = new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
// TODO anything | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
// TODO anything | |
if (TextUtils.isEmpty(s)) { | |
searchClearIcon.setVisibility(View.GONE); | |
} else { | |
searchClearIcon.setVisibility(View.VISIBLE); | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
// TODO anything | |
} | |
}; | |
/* get Suggestion Data */ | |
private void getSuggestionData() { | |
//TODO get suggestion data | |
searchSuggestionsList = ...; | |
suggestions.clear(); | |
for (Data suggestion : searchSuggestionsList) { | |
suggestions.add(suggestion.getTitle()); | |
} | |
searchSuggestionAdapter.clear(); | |
searchSuggestionAdapter.addAll(suggestions); | |
}); | |
} | |
/* close keyboard */ | |
private void closeKeyboard() { | |
//Find the currently focused view, so we can grab the correct window token from it. | |
View view = this.getCurrentFocus(); | |
//If no view currently has focus, create a new one, just so we can grab a window token from it | |
if (view == null) { | |
view = new View(this); | |
} | |
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); | |
Objects.requireNonNull(imm).hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
/* save search keyword in db */ | |
private void saveSearchKeywordInDb(String searchKeyword) { | |
//TODO save in db | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- SearchView Container --> | |
<RelativeLayout | |
android:id="@+id/searchContainer" | |
android:layout_width="match_parent" | |
android:layout_height="56dp" | |
android:layout_gravity="center_horizontal"> | |
<AutoCompleteTextView | |
android:id="@+id/searchView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/search_view_background" | |
android:hint="Search in movie title" | |
android:imeOptions="actionSearch" | |
android:inputType="text" | |
android:maxLines="1" | |
android:paddingLeft="48dp" | |
android:paddingRight="48dp" | |
android:singleLine="true" | |
android:textColor="@android:color/black" /> | |
<ImageView | |
android:id="@+id/searchIcon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentStart="true" | |
android:layout_centerVertical="true" | |
android:layout_marginStart="16dp" | |
android:tint="@color/searchViewIconTint" | |
app:srcCompat="@drawable/ic_search" /> | |
<ImageView | |
android:id="@+id/searchClearIcon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentEnd="true" | |
android:layout_centerVertical="true" | |
android:layout_marginEnd="16dp" | |
android:tint="@color/searchViewIconTint" | |
app:srcCompat="@drawable/ic_clear" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good post with lots of information. I really enjoyed every little bit of it, over all contents is must read material. I am hoping the same best work from you later on also. There are truly lots of sensible points on this website a number of my readers might realize this useful. I am thankful for this information. Keep posting, don't forget to visit my website.