Ok I'm incredibly frustrated I've been trying to get a SearchView to work for hours now with no luck. I checked out how to handle this and I found a sample project and the sample project works great. I then implement the same exact things in my project and bam I'm screwed. The intent won't fire. I don't understand why. I followed this
I have all my activities in an activity folder but removed the Search and Main one to see if that was the case... it's not. In my Search Results activity I'm handling the intent. It looks like this currently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
// get the action bar
ActionBar actionBar = getActionBar();
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
txtQuery.setText("Search Query: " + query);
}
}
My Searchable XML looks like this.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" />
For some reason the search hint is not appearing, Yet the string resource shows up and it's declared.
my options menu looks like this
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="Search"
android:icon="#android:drawable/ic_search_category_default"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="#+id/Menu"
android:title="MENU"
android:showAsAction="collapseActionView|ifRoom"
/>
And my Manifest looks like this.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MenuActivity"
android:label="#string/title_activity_menu"
android:theme="#style/IDME.Theme.Transparent" >
</activity>
<activity
android:name=".activities.CategoriesActivity"
android:label="#string/title_activity_categories" >
</activity>
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result"
>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
</application>
</menu>
And my Search View inflator looks like this.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
I cannot for the life of me figure out what's going on, I followed both guides to a tee, I cannot see what's wrong. I've messed around with metadata and I just really can't see the problem anymore. I'm stuck
I, too, followed the exact same (official Android) guide Setting Up the Search Interface and was in the same boat as the OP #rubsnick.
Turns out, all that that needs to be tweaked is--
Change
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Notice the <meta-data code! (specifically, android:name)
Also, no need to specify the searchable xml resource here... instead, do that in the SearchResultActivity activity, as shown below...
Lastly, Modify <activity for SearchResultActivity, like so:
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>
The following SO thread does full justice as to why default_searchable and not searchable:
What's the difference between android.app.default_searchable and android.app.searchable meta-data?
your meta data is incorrect you must use:
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value="your package name of searchable activity.SearchResultActivity" />
Related
I am following the google android reference https://developer.android.com/training/search/setup.html to implement my app's SearchView, but when entry completes and I click the right-bottom in the keyboard,it doesn't go to the SearchResultsActivity without error info.
My SearchView is in NotesActivity, and when user type the keyword complete,it will go to SearchResultsActivity with a recyclerView in it.
Here is my manifest.xml :
<activity
android:name=".notes.NotesActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".search.SearchResultsActivity"
android:label="#string/title_activity_search_results"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
and this is the NotesActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
Here is the SearchResultsActivity's main code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
mKeyword = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
if (!TextUtils.isEmpty(mKeyword)) {
mPresenter.loadNotes(mKeyword);
} else {
Snackbar.make(mRecyclerView, R.string.search_empty_keyword, Snackbar.LENGTH_SHORT)
.show();
}
}
}
menu/main.xml:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always"/>
xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name"/>
Please guide.
You are missing from the SearchResultsActivity the
<category android:name="android.intent.category.DEFAULT" />
and the
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
in the NotesActivity the linkage is done via
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
The searchable is related to the activity where the search dialog is displayed and the default_searchable to define which one to use for delivering the searches.
and the proper documentation for it is here https://developer.android.com/guide/topics/search/search-dialog.html
I want to create a search for the user in the ActionBar. I use the SearchView Widget as an Action View. But when I include the
part in the manifest, it also pops up a second search on top. Both the view on top and the ActionBar Search work. When I remove the part from the manifest, the second Search doesn't pop up and only the one in the ActionBar does, but nothing happens when I hit the search button.
This is how it looks:
I only want the second view not the white search on top. Perhaps somebody easily sees whats wrong.
This is my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BlueTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".helper.SearchResultsActivity" />
</activity>
<activity android:name=".helper.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
The part of the Fragment(as part of MainActivity) which handles the search:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.persons, menu);
Log.d("ml Person", "creating Options Menu for Persons");
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
getActivity().onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
widget in menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="10"
android:title="#string/action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint_persons" />
I'm trying to add a search activity using a search view in the searchbar. I've seen a few questions like this but I've tried all the answers and nothing works for me. The search show properly but when I after I type in the search phrase and click search nothing happens.
My Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.y2apps.quoteformessenger.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="android.app.default_searchable" android:resource="#xml/searchable" android:value=".SearchResultsActivity"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchResultActivity"
android:theme="#style/AppTheme"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>
</application>
the searachable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
the styles.xml file
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
The main activity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
if (searchView != null) {
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
} else {
Toast.makeText(this, "can't find search bar", Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and the search activity
public class SearchResultActivity extends Activity {
// ===========================================================
// Public Constants
// ===========================================================
// ===========================================================
// Private constants and Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Public methods
// ===========================================================
#Override
public void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
}
// ===========================================================
// Private methods
// ===========================================================
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Toast.makeText(this, "YARON YOREH 2", Toast.LENGTH_LONG).show();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, "YARON YOREH asked for " + query, Toast.LENGTH_LONG).show();
//use the query to search your data somehow
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
if it helps to diagnose this, the search hint isn't showing up either.
thank you for any help
You have a typo in your manifest:
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
I believe it should be:
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity"/>
However, you shouldn't need since you already define the default searchable globally for your application
According to your code , what i understood is your MainActivity is the search activity and you want to display the result in new activity. For that you have to make MainActivty as searchable.Change your manifest like below.
Main Activity for performing search
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Activity to display results
<activity android:name=".SearchResultActivity" ... >
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
</activity>
I am trying to make a search bar and I followed the tutorial on http://developer.android.com/training/search/setup.html. It does not work and SearchResultsActivity does not get called, so I immediately found the following question:
Search widget on action bar doesn't trigger my search activity. However, the solutions listed there (more specifically, adding meta-data inside in AndroidManifest.xml and using references to strings in xml/searchable.xml) do not solve my problem. I also tried putting meta-data in <activity> in AndroidManifest.xml and numerous other solutions to no avail.
What am I doing wrong?
Here is my code:
main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.jsondataparser.app.MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/search_menu"
android:title="#string/app_name"
android:icon="#drawable/ic_launcher"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
xml/searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/app_name">
</searchable>
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
<activity
android:name="com.jsondataparser.app.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.jsondataparser.app.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
MainActivity.java:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchResultsActivity.java:
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("SearchResultsActivity", "onCreate called");
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
Log.d("SearchResultsActivity", "onNewIntent called");
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d("SearchResultsActivity", query);
}
}
}
Put also
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
inside <activity> in your AndroidManifest.
In my application i made my content provider searchable, defining my main activity as the default search activity on my manifest.
<activity
android:name="com.pt.application.MainActivity"
android:label="#string/title_activity_map"
android:launchMode="singleTop"
android:theme="#style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Then i implemented the onSuggestionClick option to update a map, based on the suggestion clicked by the user. The searchview is on the action bar.
The problem is that when the user clicks on a suggestion, somehow the activity is recreated (i think OnPause and OnResume are called?). Is there any way to prevent this from happening? I don't want that my activity is "recreated" every time the user clicks on a suggestion.
If more code is needed, please ask. Thanks.
EDIT: Code added
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="web_search"
android:searchSuggestAuthority="com.pt.provider"
android:searchSuggestIntentData="content://com.pt.provider/busstop">
</searchable>
On my main activity:
// Bind the Activity’s SearchableInfo to the Search View
searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
this.setupSearchView();
private void setupSearchView()
{
// Use the Search Manager to find the SearchableInfo related
// to this Activity.
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
searchView.setSubmitButtonEnabled(false);
searchView.setOnSuggestionListener(new OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
// Do things
return false;
}
});
}
Solved my question. Changed on the manifest:
From:
android:launchMode="singleTop"
To:
android:launchMode="singleTask"