I have a search view as an action item. Every thing is working fine except that I want to intercept the search executed event. At that time a new activity is started with search results. When that happens I want to collaps the search view in the previous activity. So when user comes back out of the search result activity, the action menu should be collapsed.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
menu.add(Menu.NONE,Menu.NONE,1,"Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
// Search
} else {
// Do something when there's no input
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
setSupportProgressBarIndeterminateVisibility(true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
setSupportProgressBarIndeterminateVisibility(false);
}
}, 2000);
return false; }
});
return true;
}
I think you can collapse SearchView in onQueryTextSubmit method, see above code
Related
When using search view, I have a requirement from customer that they want to retain the search content after reopen the search view. My Search view is on a list view and do a real timing filtering based on what user input into the search box. When closed the search box by either click the back button on the phone or click the soft back button on the top left on action bar, the search box closed, search view iconfied. But when reopen it next time, the search query used last time is also been cleared, which I do not want.
My question is that is there a way I can keep the search view content there. Just hiding the search box, but not clear the content?
My related code are as follow:
MenuItem search;
SearchView searchView;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_locationlist_fragment, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
search = menu.findItem(R.id.action_search_location_list);
searchView = (SearchView) MenuItemCompat.getActionView(search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
//This will make sure, when user closed search view, the list will be restored.
if(!hasFocus) {
Log.i(Tags.LOCATIONLIST,"Search Close");
search.collapseActionView();
} else {
}
}
}
});
ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText searchEditText = (EditText)searchView.findViewById(R.id.search_src_text);
searchEditText.setText("");
if (((LocationListAdapter)locationListView.getAdapter())!=null) {
((LocationListAdapter) locationListView.getAdapter()).getFilter().filter("");
}
}
});
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search_location_list:
((BaseActivity) getActivity()).onSearchRequested();
return true;
case R.id.action_refresh_location_list:
refreshLocationList();
return true;
default:
return false;
}
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
if (((LocationListAdapter)locationListView.getAdapter())!=null) {
if (TextUtils.isEmpty(s)) {
locationListView.clearTextFilter();
} else {
((LocationListAdapter) locationListView.getAdapter()).getFilter().filter(s);
//locationListView.setFilterText(s.toString());
}
}
return true;
}
Use
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setIconified(false);
Any query text is cleared when iconified. So setIconified to false. And i have used android.widget.SearchView
Save your String in a variable (e.g. myWantedString) and
override setOnClickListener that trigers everytime you open the SearchView and use setQuery. Your code should be:
searchView.setOnSearchClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
searchView.setQuery(myWantedString, false);
}
});
To save your string each time the SearchView closes implement setOnCloseListener and override onClose():
searchView.setOnCloseListener(new SearchView.OnCloseListener()
{
#Override
public boolean onClose()
{
myWantedString = searchView.getQuery();
return false;
}
});
searchView.setQuery() works if was called with a delay after menu item expansion.
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
// set query text with a delay
searchView.post(new Runnable() {
#Override
public void run() {
searchView.setQuery(query, false);
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
You can create an Activity which can be called when the user searches and the search result can be stored in the Bundle during the callback method onPause or onSaveInstanceState , when the Activity is called once again restore it from the bundle.
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setIconified(false);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.setQuery("SEARCH_WORD", false);
}
});
https://developer.android.com/reference/android/widget/SearchView
Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.
I am using searchview in my application ( without action bar). How can I collapse searchview after query text submit?
I have these listeners ;
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)thisFr.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(globalSearch.getWindowToken(), 0);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
I don't use ActionBar so I don't have a function like collapseActionView().
you need to call setIconified(true) twice to actually collapse your search view, with first call text is cleared with second call keyboard and search view get closed.
You can do it this way in your activity, tested with actionbarsherlock (it even hides the keyboard, make sure to return false in onQueryTextSubmit):
private MenuItem searchMenuItem;
public MenuItem getSearchMenuItem() {
return searchMenuItem;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// ...
searchMenuItem = menu.findItem(R.id.menu_search);
// ...
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MenuItem searchMenuItem = getSearchMenuItem();
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// ...
return true;
}
});
// ...
return super.onCreateOptionsMenu(menu);
}
If you are using the SearchView in the OptionsMenu, you ca call invalidateOptionsMenu()
//close suggestion list on query text submit
searchView.setIconified(true);
final MenuItem searchterm = menu.findItem(R.id.search);
SearchView searchView = null;
searchView = (SearchView) searchterm.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchterm.collapseActionView();
}});
I am using the actionbar sherlock. Now I want to add a search option to actionbar and intent Edittext search to other activity.
can anyone help me?
Do something like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
menu.add(Menu.NONE,Menu.NONE,1,"Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
// Search
} else {
// Do something when there's no input
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Toast.makeText(getBaseContext(), "dummy Search", Toast.LENGTH_SHORT).show();
setSupportProgressBarIndeterminateVisibility(true);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
setSupportProgressBarIndeterminateVisibility(false);
}
}, 2000);
return false; }
});
return true;
}
I am currently using an ActionBar menu item to display a SearchView in the action bar. When the search menu item is expanded the soft keyboard is displayed which is what I want. Now, when the user presses the back button to close the soft keyboard, I would also like to collapse the SearchView in the action bar.
I have tried implementing the following listeners OnKeyListener and OnFocusChangeListener on the MenuItem and the ActionView. I have also tried using OnBackPressed() in the Activity. None of the above detect when the back button is used to close the soft keyboard.
Any ideas?
I have implemented OnActionExpandListener to know when the SearchView is visible.
I'll expand on #user1258568 's answer for the lazy. This worked for me. Note that it clears your query when focus is lost.
final MenuItem searchMenuItem = optionsMenu.findItem(R.id.search);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if(!queryTextFocused) {
searchMenuItem.collapseActionView();
searchView.setQuery("", false);
}
}
});
I found a better solution.
searchView.setOnQueryTextFocusChangeListener().
The OnQueryTextFocusChangeListener gets called when the keyboard is displayed or hidden. Gets called first when the keyboard is displayed and the search view will have focus. Gets called again when keyboard is hidden and search view will lose focus, can close search viewthen using
menuItem.collapseActionView().
Just Override onBackPressed like this:
#Override
public void onBackPressed() {
if (searchView.isShown()){
searchView.onActionViewCollapsed(); //collapse your ActionView
searchView.setQuery("",false); //clears your query without submit
isClosed = true; //needed to handle closed by back
} else{
super.onBackPressed();
}
}
and your onCreateOptionsMenu would inflate the mSearchView like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
mSearchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnSearchClickListener(this);
mSearchView.setOnCloseListener(this);
isClosed = true;
return true;
}
have you class implement the following like this:
public class myActivity extends FragmentActivity implements
SearchView.OnQueryTextListener, View.OnClickListener, SearchView.OnCloseListener {
which you will also need:
#Override
public void onClick(View view) {
isClosed = false;
}
#Override
public boolean onClose() {
isClosed = true;
return false;
}
You will need to make "mSearchView" and "isClosed" both global variables to the activity.
The answer from Jon Willis works great. This is an improvement to his answer.
First, create a new class that implements View.OnFocusChangeListener:
public class SearchViewFocusListener implements View.OnFocusChangeListener {
private final MenuItem mMenuItem;
public SearchViewFocusListener(MenuItem menuItem) {
mMenuItem = menuItem;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
mMenuItem.collapseActionView();
if (v instanceof SearchView) {
((SearchView) v).setQuery("", false);
}
}
}
}
Next, set the listener on your SearchView:
searchView.setOnQueryTextFocusChangeListener(new SearchViewFocusListener(menuItem));
You only need to put the "collapseActionView" attribute in the menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_search"
android:title="#string/search"
android:iconifiedByDefault="true"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/> <--this one
</menu>
That will give you the functionality you look for all by itself.Don't forget to call the method "clearFocus" on the SearchView to close the keyboard once you send the query.
This is what I did for making the keyboard disappear. You can try to see if this works for you. I set the searchView to invisible and then to visible again.
//set query change listener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure keyboard disappears when query is submitted
*/
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
return false;
}
});
It's achievable like this:
private void setupSearchView(Menu menu) {
final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
[...]
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchMenuItem.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
}
Solutions based on setOnQueryTextFocusChangeListener() did not work for me because the event was not launched - the searchView did not lose focus when submitted, probably because I perform the search in the same activity that contains the Search View.
Anyway, I think using OnQueryTextListener is more correct, as it describes the event of submitting text more precisely.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home_screen, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchMenuItem
.getActionView();
searchView.setIconifiedByDefault(false);
if (searchManager != null && searchView != null) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}// end if
if (searchView != null) {
searchView.setQuery("", false);
}// end if
}// end if
}
});
searchView
.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure
* keyboard disappears when query is submitted
*/
if (searchView != null) {
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
If you want to collapse keyboard when user clicks search icon on keyboard
this can be achieved by simple
inside onquerytextsubmitted {
searchView.clearfocus()
}
You need to call setIconified twice.
To actually collapse your search view and close the keyboard.
With first call text of search view is cleared with second call keyboard and search view get closed.
For some reason, menuItem.collapseActionView() did not work so I used searchView.setIconified(true) instead.
This gives the below result as the code sample.
final MenuItem searchItem = (MenuItem) menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextFocusChangeListener(new SearchView.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
searchView.setIconified(true);
}
}
});
I have a searchView in the ActionBar. I want to dismiss the keyboard when the user is done with input. I have the following queryTextListener on the searchView
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
Based on similar questions, the following code should dismiss the keyboard, but it doesn't work in this case:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I've also tried:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Neither one works. I'm not sure if this is a Honeycomb specific problem or if it's related to the searchView in the ActionBar, or both. Has anyone gotten this working or know why it does not work?
I was trying to do something similar. I needed to launch the SearchActivity from another Activity and have the search term appear on the opened search field when it loaded. I tried all the approaches above but finally (similar to Ridcully's answer above) I set a variable to SearchView in onCreateOptionsMenu() and then in onQueryTextSubmit() called clearFocus() on the SearchView when the user submitted a new search:
private SearchView searchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView(); // set the reference to the searchView
searchView.setOnQueryTextListener(this);
searchMenuItem = (MenuItem) menu.findItem(R.id.menu_search);
searchMenuItem.expandActionView(); // expand the search action item automatically
searchView.setQuery("<put your search term here>", false); // fill in the search term by default
searchView.clearFocus(); // close the keyboard on load
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
performNewSearch(query);
searchView.clearFocus();
return true;
}
Simple, straight to the point and clean:
#Override
public boolean onQueryTextSubmit(String query) {
// your search methods
searchView.clearFocus();
return true;
}
just return false on onQueryTextSubmit just like below
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
Somehow it works if you call
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
and then
otherWidget.requestFocus();
For me, none of the above was working on the first submit. It was hiding and then immediately re-showing the keyboard. I had to post the clearFocus() on the view's handler to make it happen after it was done with everything else.
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (!"".equals(query)) {
mSearchView.post(new Runnable() {
#Override
public void run() {
mSearchView.clearFocus();
}
});
}
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
For me, the following works:
In my activity I have a member variable
private SearchView mSearchView;
In onCreateOptionsMenu() I set that variable like so:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.library, menu);
mSearchView = (SearchView)menu.findItem(R.id.miSearch).getActionView();
mSearchView.setOnQueryTextListener(this);
return true;
}
In the QueryTextListener at last, I do this:
mSearchView.setQuery("", false);
mSearchView.setIconified(true);
I had a look at the source code of SearchView, and if you do not reset the query text to an empty string, the SearchView just does that, and does not remove the keyboard neither. Actually, drilling down deep enough in the source code, it comes to the same, yuku suggested, but still I like my solution better, as I do not have to mess around with those low level stuff.
Edit: I added the better solution on top, but also kept the old answer as a reference.
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return false;
}
Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.
//set query change listener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure keyboard disappears when query is submitted
*/
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
return false;
}
});
if someone is looking how to collpase searchView/keyboard, use below code
/*
setup close button listener
*/
searchView.findViewById<ImageView>(R.id.search_close_btn).setOnClickListener {
adapter.filter(null)//reset default list
searchView.onActionViewCollapsed() //collapse SearchView/Keyboard
true
}
/*
setup text change listener
*/
searchView.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
adapter.filter(if(query.isNullOrEmpty()) "" else query)
searchView.onActionViewCollapsed() //collapse SearchView/Keyboard
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter(if(newText.isNullOrEmpty()) "" else newText)
return true
}
})
In a tablet app I'm working on with a dual pane activity, I've wrote only
f.getView().requestFocus(); // f is the target fragment for the search
and that was enough to dismiss the soft keyboard after a search. No need to use InputMethodManager
I used ActionBarSherlock 4.3 and I have a ProgressDialog. When I dismiss it in postExecute method, Searchview gain focus. To fix that:
//Handle intent and hide soft keyboard
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
searchView.setQuery("", false);
searchView.setIconified(true);
searchView.clearFocus();
}
/*When dismiss ProgressDialog, searchview gain focus again and shows the keyboard. I call clearFocus() to avoid it.*/
AsyncTask.onPostExecute(){
if(pd!=null)
pd.dismiss();
if(searchView!=null)
searchView.clearFocus();
}
Hope it helps.
You can use it.
if (isKeybordShowing(MainActivity.this, MainActivity.this.getCurrentFocus())) {
onBackPressed();
}
public boolean isKeybordShowing(Context context, View view) {
try {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(view.getWindowToken(), 0);
return keyboard.isActive();
} catch (Exception ex) {
Log.e("keyboardHide", "cannot hide keyboard", ex);
return false;
}
}
Two solutions that worked for me, the first one using the SearchView instance:
private void hideKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
}
Second solution:
private void hideKeyboard(){
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
Below Code is working for me to hide keyboard in Searchview
MenuItem searchItem = baseMenu.findItem(R.id.menuSearch);
edtSearch= (EditText) searchItem.getActionView().findViewById(R.id.search_src_text);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
edtSearch.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.
ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.search_src_text);
et.setText("");`enter code here`
searchView.setQuery("", false);
searchView.onActionViewCollapsed();
menuSearch.collapseActionView();
}
});
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
#Override
public void onResume() {
super.onResume();
searchView.clearFocus();
}
I tried all the answers above but none worked, clearing focus onResume worked for me