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.
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
Here is my code for searchview in android.Search option is visible on the toolbar but when a text and entered and searched it does not trigger SearchActivity.
xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/label"
android:hint="#string/search" >
</searchable>
menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.welcomecure.searchview">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<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=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/app_name">
<!-- to identify this activity as "searchable" -->
<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>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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, 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(getComponentName()));
return true;
}
}
SearchActivity.java
public class SearchActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.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;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}
Your Activity is named SearchActivity, but you declared SearchResultsActivity in manifest:
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
From the docs:
To declare the searchable activity for an activity's search dialog,
add a element inside the respective activity's
element. The element must include the android:value
attribute that specifies the searchable activity's class name and the
android:name attribute with a value of
"android.app.default_searchable".
try to use the same activity name from the calling activity, you used SearchActivity to show the search result, try to use the same activity name
<activity android:name=".MainActivity">
<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>
You can also do this programmatically.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) { // this is triggered when you press the key search from keyboard.
return true;
}
#Override
public boolean onQueryTextChange(String newText) { // this is triggered when you change your search text.
return true;
}
});
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" />
Hi i want to implement search functionality in my app over toolbar.Currently i have added SearchView element in activity_main.xml , but I don't know how to implement remaining logic. Please guide me to solve this
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CC33"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="#string/app_name">
<SearchView
android:id="#+id/mySearchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="true"
android:layout_gravity="end"
android:queryHint="Search by name"/>
</android.support.v7.widget.Toolbar>
my AndroidManifest.xml is
<application
android:allowBackup="true"
android:icon="#drawable/splash_img"
android:label="Call Logger"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.comforters.callLogger.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.comforters.callLogger.MainActivity"
android:label="Call Logger" >
</activity>
<receiver android:name=".PhonecallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
Create a menu item inside your menu folder say /main/res/menu have the following
<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=".MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</item>
That menu should be bound to the activity that will handle the search. In your Activity have something like this
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_activity_main, menu);
super.onCreateOptionsMenu(menu, inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(context.getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextChange(String query)
{
adapter.getFilter().filter(query);
return true;
}
#Override
public boolean onQueryTextSubmit(String query)
{
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
Then in your adapter implement you filter logic, this will depend on what you are searching and also on how the data is presented
Hope this points you in the right direction
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" />