How to add extra data to android.widget.SearchView? - android

I have defined my SearchView like this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater)
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getActivity().getComponentName());
SearchView searchView = (SearchView) menu.findItem(R.id.action_menu_search).getActionView();
searchView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
searchView.setSearchableInfo(info);
}
Everything works fine. When I submit query new intent is fired and I capture it in my MainActivity. But I do not know where the search query is coming from.
I can see mAppSearchData variable that could help me in android.widget.SearchView, but it is not accessible for some reason - code searchView.setAppSearchData(bundle); does not compile.
Is there some other way to pass pass additional data to detect where the search is coming from?

You can implement your own OnQueryTextListener e.g.:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtra(MyActivity.IMPORTANT_NUMBER, importantNumber);
intent.putExtra(SearchManager.QUERY, s);
intent.setAction(Intent.ACTION_SEARCH);
startActivity(intent);
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
More Details: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/SearchView.java
It is important that you return true in onQueryTextSubmit, so the searchView does not perform the default action.

You could send the activity name with the query by appending the query. Let's say your query is 'hello', you would append it to something like that 'mainactivity%%hello'. In your search activity, you can then parse the query to retrieve the activity name. Consider this solution has a workaround. There's probably something better out there.

Related

Make the SearchView do searches

I have a SearchView up and ready, here is the code I use in the java file of the page that has the SearchView:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(), SearchableActivity.class)));
return true;
}
My goal is now to let this SearchView search through a site full of TextViews. Every TextView contains one word. Is it possible to make the SearchView find the TextView that contains the word i just typed in? If so, how? If not, how can I make it happen in another way?
Kind regards,
Julian
The SearchView doesn't do any search at all, it's a simple widget for inputting the keywords, show recently used keywords, etc. You should implement your "search model" to find the content, and display it somewhere you want.
I guess what you are trying to do is search through text inside TextViews defined inside your layout.
I am assuming here that the text source is a String Array/ArrayList (TextViewData) in a file "Data.java.
You can give this a try:
1) Make the activity/fragment implement SearchView.OnQueryTextListener
2) Create an empty ArrayList to store search entry results.
private ArrayList<Data> mSearchItems
3) Add this line to the code where you initialize your SearchView
searchView.setOnQueryTextListener(this);
4) Override the following methods:
i) Method called on submit:
#Override
public boolean onQueryTextSubmit(String query) {
//Call a user defined mathod to handle the search.
handleSearch(query, Data.TextViewData)
}
ii) Method called on changing the text in the search box
#Override
public boolean onQueryTextChange(String query) {
handleSearch(query, Data.TextViewData)
}
5)
private void handleSearch(String Query, ArrayList<Data> queryList){
mSearchItems.clear();
for(Data data : queryList){
if(data.textviewone.toLowerCase().contains(Query.toLowerCase()) ||
data.textviewtwo.toLowerCase().contains(Query.toLowerCase()))
mSearchItems.add(data);
//You can then swap the recycler view (if being used) or hide other text views and display only the one being searched for (in case of a match)
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
SearchManager searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Setting up custom search button icon
int searchImgId = android.support.v7.appcompat.R.id.search_button; // I used the explicit layout ID of searchview's ImageView
ImageView v = (ImageView) searchView.findViewById(searchImgId);
v.setImageResource(R.drawable.ic_search_black);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Do operation
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
style="#android:style/Widget.ActionButton"
android:icon="#drawable/ic_search_black"
android:title="#string/search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always" />
</menu>

How to implement a Searchview in android?

I added some code to oncreate options menu like this
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
Also I created a xml file in xml directory and I did what is said in this.But I don't know how to pass search query and get the result. Executing this getting action bar disappeared.
Furthermore if you want have some callback use the code below, on putting some query and fetch it.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
Android Search View Example in android Working Source Code
https://drive.google.com/open?id=0BzBKpZ4nzNzUajJhNEV2N25qWlU

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();

How to prevent SearchView from auto-clearing the searched text

Normal Behavior:
When I request search in the SearchView widget on ActionBar, after
clicking soft-keyboard's search action button, the input text is
cleared from the SearchView.
My desired behavior:
I want the my input text to remain the same, when I perform search. How is that possible?
I have read official docs for SearchView, and guides from here, and also here, but haven't had any luck.
EDIT:
I discovered that this text reset happens whenever I scroll my ViewPager.
Try this:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String query)
{
mSearchView.setQuery(query,false);
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
});
If your search widget calls another activity to handle the query, it gets cleared because the activity is newly created.
Therefore you need to set the query manually from 2 places:
onCreateOptionsMenu where you initialize your search widget
onNewIntent where you receive your query
Why from 2 places? Because if the search-handling activity is newly created, onNewIntent is called first and the search widget is not yet ready to be used. In that case save the query at onNewIntent and set it at onCreateOptionsMenu. Otherwise it can be directly set at onNewIntent.
Here's an example:
private String mQuery;
private SearchView mSearchView;
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mQuery = intent.getStringExtra(SearchManager.QUERY);
if (mSearchView != null) {
mSearchView.setQuery(mQuery, false);
}
// Do something with the new query
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the actionbar items
getMenuInflater().inflate(R.menu.actionbar_items, menu);
// Get SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Disable collapsing the search widget
mSearchView.setIconifiedByDefault(false);
// Set the query
mSearchView.setQuery(mQuery, false);
return true;
}

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;
}
});
}

Categories

Resources