I'm using SearchView while my activity is extanding Sherlock Library.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.countriesxml, menu);
SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
.getActionView();
mSearchView.setQueryHint("Search for smartphones");
MenuItem menuItem = menu.findItem(R.id.searchall);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
item.collapseActionView();
indicator.setVisibility(View.VISIBLE);
pager.setVisibility(View.VISIBLE);
lvth.setVisibility(View.GONE);
return true;
}
});
return;
}
Please note that i tried to use OnCloseListener() but it didn't work.
What's wrong?
have a look at your code at this section:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
Replace it with below code and try again.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
Try using OnFocusChangeListener instead
menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
// Code here
}
}
});
Related
I am trying to set visibility of SaveMessage true after clicking on UnsaveMessage and visibility of Unsavemessage false but i only see on item and nothing happens on Onclick
My code
private boolean isEditing = true;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_message,menu);
menu.findItem(R.id.UnsavedMessage).setVisible(isEditing);
menu.findItem(R.id.SaveMessage).setVisible(!isEditing);
return super.onCreateOptionsMenu(menu);
}
#Override
public void supportInvalidateOptionsMenu() {
super.supportInvalidateOptionsMenu();
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == R.id.UnsavedMessage){
isEditing=false;
}
if (item.getItemId()==R.id.SaveMessage){
isEditing=true;
}
return true;
}
You need to do this
private boolean isEditing = true;
private MenuItem unSavedMsg;
private MenuItem SaveMsg;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_message,menu);
unSavedMsg = menu.findItem(R.id.UnsavedMessage)
unSavedMsg .setVisible(isEditing);
saveMsg = menu.findItem(R.id.SaveMessage)
saveMsg .setVisible(!isEditing);
return super.onCreateOptionsMenu(menu);
}
#Override
public void supportInvalidateOptionsMenu() {
super.supportInvalidateOptionsMenu();
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == R.id.UnsavedMessage){
isEditing=false;
// Show/Hide Btn here
unSavedMsg.setVisibility(false)
saveMsg.setVisibility(true)
}
if (item.getItemId()==R.id.SaveMessage){
isEditing=true;
// Show/Hide Btn here
unSavedMsg.setVisibility(true)
saveMsg.setVisibility(false)
}
return true;
}
I have some code that sets a boolean if the search view is open or not.
MenuItemCompat.setOnActionExpandListener(action_search, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item){
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item){
isSearch = false;
return true;
}
});
It works. I'm happy with the way it responds. However, it it shows as deprecated, the setOnActionExpandListener is crossed out with the warning
android.support.v4.view.MenuItemCompat.setOnActionExpandListener is deprecated
Suggestions?
android.support.v4.view.MenuItemCompat.setOnActionExpandListener is deprecated
Yes MenuItemCompat.setOnActionExpandListener This method was deprecated in API level 26.1.0.
Suggestions?
Use MenuItem.setOnActionExpandListener(MenuItem.OnActionExpandListener) directly.
MenuItemCompat.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed. The menu item must be configured to expand or collapse its action view using the flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW.
SAMPLE CODE
MenuItem item = menu.findItem(R.id.action_order);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
isSearch = false;
return true;
}
});
This interface was deprecated in API level 26.1.0.
Instead of MenuItemCompat.OnActionExpandListener
Use MenuItem.OnActionExpandListener directly.
Official Documentation
Sample:
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
menuItemSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
isSearch = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
isSearch = false;
return true;
}
});
Bonus
setting OnQueryTextListener and OnCloseListener
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) menuItemSearch.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//Do something
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// do something
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
I don't want search(newText) to be executed when searchview collapses.
I tried
if(searchView.isShown) search(newText);
and
if(!searchView.isIconified) search(newText);
but when I close the searchview, searchView.isShown and searchView.isIconified values will be updated after triggering onQueryTextChange.
My onCreateOptionMenu code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contacts, menu);
addContactMenuItem = menu.findItem(R.id.action_add_contact);
searchViewMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchViewMenuItem);
MenuItemCompat.setOnActionExpandListener(searchViewMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
addContactMenuItem.setVisible(false);
if (contactListAdapter != null)
contactListAdapter.getFilter().filter(searchView.getQuery());
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addContactMenuItem.setVisible(true);
contactListAdapter.performGroupBasedFiltering(groupSpinner.getAdapter().getItemId(groupSpinner.getSelectedItemPosition()));
return true;
}
});
return true;
}
Please help me out! Thanks in advance.
Try this use setOnActionExpandListener
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// perform your action here
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// perform your action here
return true;
}
});
return true;
}
EDIT
as per your comment
I need search(newText) not to be executed on collapsing.
than set this searchView.setOnQueryTextListener(null); when you don't need
I want to handle collapsing state but I can not handle onActionExpandListener. I use actionbarsherlock and here are my codes. Where do I make mistake?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
menu.getItem(0).setVisible(aramaOnOff);
menu.getItem(1).setVisible(filterOnOff);
SearchView searchView = (SearchView)menu.findItem(R.id.imgBuyutec).getActionView();
searchView.setOnQueryTextListener(this);
searchViewItem = menu.findItem(R.id.imgBuyutec);
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
return true;
}
Use appcompat-v7 , http://developer.android.com/guide/topics/ui/actionbar.html
and then use the following code,
MenuItemCompat.setOnActionExpandListener(searchViewItem,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//whatever
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//whatever
return true;
}
});
I am using SearchView and it is working fine but only setOnCloseListener is not working; Here is my code
import com.actionbarsherlock.widget.SearchView.OnCloseListener;
and
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
return false;
}
});
**EDIT****
#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");
searchView.setIconifiedByDefault(true);
//search button
menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
MenuItem sView = menu.findItem(1);
sView.setOnActionExpandListener(this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adopter.getFilter().filter(null);
Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
return true; // Return true to expand action view
}
Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu
// searchView.setOnCloseListener(new OnCloseListener() {
// #Override
// public boolean onClose() {
// adapter.getFilter().filter("");
// Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show();
// return false;
// }
// });
MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
});
I have encountered the same problem with onCloseListener not invoking for the SearchView.
Understand from the bug issue raised in 25758, and some postings I have read, to invoke onCloseListener, you need to set:
searchView.setIconifiedByDefault(true);
But for my case I wanted to have the search view opened & not iconified all the time. I manage to resolve this by adding one more line below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_bar, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);
searchView.setIconifiedByDefault(true);
searchView.setIconified(false);
return true;
}
The searchView.setIconified(false) will cause the searchView to open up, despite setting the default to iconified to true in the previous line. In this way, I managed to have both a SearchView
that opens up all the time & having it invoke the onCloseListener.
I'm using this code. And it works perfectly
#Override
public void onStartSearch() {
}
#Override
public void onSearch(String search) {
}
#Override
public void onCloseSearch() {
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
// Associate searchable configuration with the SearchView
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); //here I user appcompat, but you can take it just from actionbarsherlock
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setTextColor(getResources().getColor(R.color.action_bar_text_color));
mSearchView.setIconifiedByDefault(true);
mSearchView.setSubmitButtonEnabled(false);
/**
* Set all of different kinds of listeners
*/
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menuIsOpen = false;
onCloseSearch();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
menuIsOpen = true;
onStartSearch();
return true;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String s) {
onSearch(s);
return false;
}
#Override
public boolean onQueryTextSubmit(String s) {
onSearch(s);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
/**
* Function for closing search when android is less than 14
*/
public boolean onBackButton() {
if (AndroidUtils.getSdkVersion() >= 14)
return false;
if (menuIsOpen) {
menuIsOpen = false;
getActivity().supportInvalidateOptionsMenu();
onCloseSearch();
return true;
}
return false;
}
After backButton it close search, or just by selecting close from action bar.
You can use a OnActionExpandListener:
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
SearchView searchView = new SearchView(((SherlockFragmentActivity) getActivity()).getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(true);
// ...
MenuItem menuItem = menu.add(R.string.search);
// ...
menuItem.setOnActionExpandListener(this);
}
#Override
public boolean onMenuItemActionExpand(final MenuItem item) {
mInSearchMode = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
mInSearchMode = false;
return true;
}