android searchView in actionbar filter issue - android

i have searchview in the action bar it is not working i try to do this like as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
return true;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
but it give me error that android.support.v7.widget.SearchViewOnQueryTextListener in searchview can not be apply

#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
Return should be at the end,
last three braces where not in correct order.

Related

Action bar search view in fragment is not working

Here is what I did so far -
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
getActivity().setTitle("DASHBOARD");
SearchManager searchManager = (SearchManager)getContext(). getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtadapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
Here code Search view in action bar, it only filter the one name other does not work . The code adapter class code is below . Actionbar doing in fragment
adapter = new FavouritesAdapter(getContext(),distanceArr,contactName,status,leadDetailsId,addressLine1,addressLine2,city,state,zipCode);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);

How to use searchable.xml

I read lots of posts on this but I actually don't understand how it works:
I created a searchable.xml in the xml folder:
Then I referenced it to my Manifest like this:
I can't figure out how can I use it in the search code, I tried with the SearchManager but without success:
SearchView searchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) myActionMenuItem.getActionView();
ComponentName componentName = new ComponentName(this, MainActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
EventBus.getDefault().post(new SearchQueryEvent(s));
return false;
}
});
return true;
}

Start app with SearchView widget open

Hello i need to put some text inside the searchView and focus/expand the searchView widget.
this is what i tried but it doesn't work. Only the keyboard is open.
Yes, I check the other solutions in the web
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_busqueda, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQuery("test",true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
Log.d("dimar","on text chnge text: " + newText);
List<Producto> lista = new Select().from(Producto.class).execute();
List <Producto> lista2 = new ArrayList<Producto>();
for (int i=0;i<lista.size();i++)
{
if (lista.get(i).getNombre().contains(newText)||lista.get(i).getLocal_id().contains(newText))
{
lista2.add(lista.get(i));
}
}
AdaptadorCuentas adap = new AdaptadorCuentas(MainActivity.this,lista2);
listView.setAdapter(adap);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
Log.d("dimar", "on query submit: " + query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
The correct code
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchMenuItem.expandActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();

SearchView in actionBar

I've followed this code to implement SearchView inside ActionBar (as an item inside the menu).
But when i reach this line
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
getActionView() return a null value.
I can't figure out what is wrong.
Try replacing
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
with
mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchMenuItem);
Try this..
private SearchView mSearchView;
....
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView(MenuItem searchItem) {
if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextChange(String newText) {
//Query
return false;
}
public boolean onQueryTextSubmit(String query) {
//query
return false;
}
public boolean onClose() {
return false;
}
protected boolean isAlwaysExpanded() {
return false;
}
And don't forget to implement the searchview...
implements SearchView.OnQueryTextListener

Setting Background for Actionbar SearchItem

I would like to set the background (something like EditText Background) to my ActionBar SearchItem. That's the current action bar.
I would like to set this background drawable to that.
Here's how I create the searchItem
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.guest_list, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager = (SearchManager) getActivity()
.getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconified(true);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
setFilter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
setFilter(query);
return false;
}
});
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconified(true);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
setFilter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
setFilter(query);
return false;
}
});
}
}
I've tried setting background by searchView.setBackgroundDrawable but I am not getting what I like to implement.
Please check below link because it explains everything about searchbar in actionbar:
Changing the background drawable of the searchview widget

Categories

Resources