How To Make Searchview Lose Focus And Collapse When Clicking Elsewhere On Activity
Solution 1:
Well I found out the following solution. I used setOnTouchListener on every view that is not an instance of searchview to collapse the searchview. It worked perfect for me. Following is the code.
publicvoidsetupUI(View view) {
if(!(view instanceof SearchView)) {
view.setOnTouchListener(newOnTouchListener() {
publicbooleanonTouch(View v, MotionEvent event) {
searchMenuItem.collapseActionView();
returnfalse;
}
});
}
//If a layout container, iterate over children and seed recursion.if (view instanceof ViewGroup) {
for (inti=0; i < ((ViewGroup) view).getChildCount(); i++) {
ViewinnerView= ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
This is the answer I referred to.
Solution 2:
Well, I have another simpler and tidier way of doing this. If you're using search widget as an action view in the toolbar (https://developer.android.com/guide/topics/search/search-dialog#UsingSearchWidget), you might want to make the SearchView lose focus and hide the keyboard when user touches anywhere else on the screen.
Instead of iterating and setting up touch listeners on every view in the hierarchy except the SearhView, you can simply make use of this solution since the SearchView has a AutoCompleteTextView in it.
Step 1: Make the parent view(content view of your activity) clickable and focusable by adding the following attribute
android:clickable="true"android:focusableInTouchMode="true"
Step 2: Set OnFocusChangeListener on the SearchView AutoCompleteTextView
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
finalMenuItemsearch= menu.findItem(R.id.action_search);
SearchViewsearchView= (SearchView) search.getActionView();
// get a reference of the AutoCompleteTextView from the searchViewAutoCompleteTextViewsearchSrcText= searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchSrcText.setOnFocusChangeListener(newView.OnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManagerinputMethodManager= (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
returnsuper.onCreateOptionsMenu(menu);
}
That's it! Hopefully this is useful for future readers :)
Solution 3:
in my case, i had to stop the search and close the keyboard, whenever user clicks on any other view, this worked for me.
publicstaticvoidhideSoftKeyboard(Activity activity) {
InputMethodManagerinputMethodManager= (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
@OverridepublicbooleandispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Viewview= getCurrentFocus();
if (view != null && view instanceof EditText) {
Rectr=newRect();
view.getGlobalVisibleRect(r);
intrawX= (int) ev.getRawX();
intrawY= (int) ev.getRawY();
if (!r.contains(rawX, rawY)) {
hideSoftKeyboard(MainActivity.this);
}
}
}
returnsuper.dispatchTouchEvent(ev);
}
Solution 4:
This works for me, mOptionsMenu is saved in onCreateOptionsMenu:
publicvoidsetupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.if(!(view instanceof EditText)) {
view.setOnTouchListener(newView.OnTouchListener() {
publicbooleanonTouch(View v, MotionEvent event) {
hideSoftKeyboard(MainActivity.this);
if(mOptionsMenu == null) returnfalse;
MenuItemsearchMenuItem= mOptionsMenu.findItem(R.id.action_search);
if(searchMenuItem == null) returnfalse;
((SearchView)searchMenuItem.getActionView()).clearFocus();
returnfalse;
}
});
}
//If a layout container, iterate over children and seed recursion.if (view instanceof ViewGroup) {
for (inti=0; i < ((ViewGroup) view).getChildCount(); i++) {
ViewinnerView= ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
publicstaticvoidhideSoftKeyboard(Activity activity) {
InputMethodManagerinputMethodManager= (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Post a Comment for "How To Make Searchview Lose Focus And Collapse When Clicking Elsewhere On Activity"