Functioning the searchview in actionbarsherlock - android

I have a sherlockactivity that implements an onItemClickListener. Then I create a new ListView and put data in it, I want to create a search at the top in the actionbarsherlock like a lot of famous apps.
This is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView searchView = new
SearchView(getSupportActionBar().getThemedContext());
menu.add("Search")
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
currently, the word "search" appears at the top of my actionbarsherlock. If I click on it, a textfield will appear in the actionbarsherlock. But when I type anything or click search (In the keyboard) nothing happens. What am I missing?

These two are the methods you need to override to control when the user types on the SearchView and when the user clicks on the search button:
public class MyClass extends SherlockActivity implements SearchView.OnQueryTextListener{
...
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
Then you need
searchView.setOnQueryTextListener(this);

Related

Using single SearchView in Activity, for different fragments

I'm trying to use one searchview from actitivity toolbar menu, to filter three fragments attached to it (It's a tabbed activity) at the same time and categorizing the results in the different fragments . Kind of like the way Instagram does theirs. I've tried inflating the onCreateOptionsMenu in each fragment, but this just starts a new instance of the search i.e (search icon is .istIconified(); I want the differnt tabs to show the query text of what ever was typed in it and perform the search at the same time.Can't seem to find this solution on SO, Any help or resource will be very much appreciated
Well it is possible.
first of all use "setOnQueryTextListener"
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.i("well", " this worked");
return false;
}
});
}
After that pass this string to currently selected fragment by calling fragment method from the activity. click here to check how to call fragment method from activity
From fragment method you can do anything which you want to do for.

Show dropdown when onQueryTextChange

I have added a searchView in my toolbar and I would like to show a dropdown as android does with the AutoCompleteTextView.
So basically I want to display a list of result and change it while the user is typing. I have added already a function to filter my results, but I have no clue how to show the dropdown in the list using my searchView.
Take a look the code below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_subs, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//TODO: Implement dropdown
_list = _repository.filterItems(newText);
return false;
}
});
return true;
}
NOTE: something like in the image, I want to show my results(list of strings) in the dropdown.
any idea?
Thanks!
you should add this code in your onCreateOptionsMenu
SearchAutoComplete autoCompleteTextView = (SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
if (autoCompleteTextView != null) {
autoCompleteTextView.setDropDownBackgroundResource(R.drawable.abc_search_dropdown_light);
}
For more information check this link
How to build Gmail like search box in the action bar?

SearchView to perform search inside same activity

I have a searchView in my SearchActivity and it creates an intent to launch a SearchActivityResults according to the documentation here: http://developer.android.com/training/search/setup.html
How do I need to go if I want my SearchView to trigger a search from within my activity, when the soft keyboard lower right button is pressed to trigger the search?
you just need to get your searchView menu , use override onCreateOptionsMenu
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(mSearchMenuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// do your search on change or save the last string in search
return false;
}
});
// you can get query
searchView.getQuery();

remove search query after pressing on search button

I used Android search widget in action bar. When I write my query and press on search button or done, everything works in my search part, but I want to remove that query in search field, or Resetting Search Widget (SearchView) value.
For example: if I write "test" after pressing search button I don't want to have this "test" in my search filed. Would you please let me know how can I remove my search query!
Thanks in advance!
I'm using SearchView widget from android
The method you are looking for is setQuery(), documented here:https://developer.android.com/reference/android/widget/SearchView.html#setQuery(java.lang.CharSequence, boolean)
Here's some example code which clears the search bar after a search is performed. You probably already have an implementation of onCreateOptionsMenu() in your activity or fragment, so just use the bits you are missing.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
// Find search item
MenuItem item = menu.findItem(R.id.my_search_item);
// Get search view attached to item
final SearchView searchView = (SearchView) mSearchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Perform search here!
performSearch(query);
// Clear the text in search bar but (don't trigger a new search!)
searchView.setQuery("", false);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
}

Handling hardware search to show searchview in ActionbarSherlock

I'm wondering how to do that : when the user press hardware search button, it's open a search view in the actionbar.
Basically I have an activity hosting fragments.
One fragment add to the ActionbarSherlock a searchview, it's working fine:
#Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
MenuInflater inflater) {
if(searchView==null)
searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
searchView.setQueryHint(getString(R.string.search));
searchView.setOnQueryTextListener(this);
menu.add(Menu.NONE, R.string.search, Menu.NONE, R.string.search)
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
super.onCreateOptionsMenu(menu, inflater);
}
I think I can also catch the hardware key in the FragmentActivity with
#Override
public boolean onSearchRequested() {
//DO SOMETHING
return super.onSearchRequested();
}
But I do not see how to open the searchview when the hardware search button is pressed.
Any hint ?
Thanks :).
So, here how I managed to use the hardware search key to open the searchview in the sherlock actionbar :
In my SherlockFragmentActivity :
private Menu ABSMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
ABSMenu = menu;
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onSearchRequested() {
MenuItem mi = ABSMenu.findItem(R.string.search); // R.string.search is the id of the searchview
if(mi!=null){
if(mi.isActionViewExpanded())
{
mi.collapseActionView();
}else
{
mi.expandActionView();
}
}
return super.onSearchRequested();
}
You can expand the search view and collapse it like
searchView.setIconified(false);
searchView.setIconified(true);
This way you can programatically invoke search view

Categories

Resources