@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
******************************************************************************
for my case
my Toolbar is
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
******************************************************************************
for my case
my Toolbar is
private void actionBarView(){ toolbar = (Toolbar) findViewById(com.mafiree.salezstorm.R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Tittle"); /* if(toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setTitle("My custom toolbar!"); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); }*/ //back button action ImageView imageview_back=(ImageView)toolbar.findViewById(com.mafiree.salezstorm.R.id.imageview_back); imageview_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Toast.makeText(getApplicationContext(),"back clicked",Toast.LENGTH_LONG).show(); finish(); } }); textview_property=(TextView)findViewById(R.id.textview_property_list); // textview_property.setVisibility(View.GONE); textview_property.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); }
my Option menu
MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.property_menu,menu); //http://www.mysamplecode.com/2012/11/android-expandablelistview-search.html MenuItem searchItem = menu.findItem(R.id.menu_search); searchView = (SearchView) searchItem.getActionView(); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); searchView.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Toast.makeText(getApplicationContext(),"back clicked",Toast.LENGTH_LONG).show(); } }); searchView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"back clicked",Toast.LENGTH_LONG).show(); Log.d("************************************searchView**","-----"); } }); searchView.setOnSearchClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"back search",Toast.LENGTH_LONG).show(); //setItemsVisibility(menu, searchItem, false); textview_property.setVisibility(View.GONE); } }); searchView.setOnCloseListener(new OnCloseListener() { @Override public boolean onClose() { textview_property.setVisibility(View.VISIBLE); Toast.makeText(getApplicationContext(),"back close",Toast.LENGTH_LONG).show(); return false; } }); searchView.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { Log.d("*********************************","------"+query); if(null != arrayList || arrayList.size() > 0){ adapter=new PropertyAdapter(getApplicationContext()); listView.setAdapter(adapter); adapter.getFilter().filter(query); } return false; } @Override public boolean onQueryTextChange(String newText) { Log.d("*********************************","------"+newText); return false; } });