I'm working around with a searchable action bar. And I gotta a problem that the action bar doesn't react with the searchable activity. THAT IS, I enter something in the action bar's searchable textedit then clicked the submit but nothing happened. The debug tracking shows that the code in my searchresultactivity is never executed. So I'm wondering if there's something wrong in searchable configuration. Well this time I think I've explained the problem clearly and I don't expect any negative votes without any words even spits!
I followed the instruction of developer's document beginning with the manifest.xml, the meta-data is added in the searchresult activity:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".GermanDictionaryActivity"
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=".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>
then I initialized the searchview with my menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.srhbar, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSrhView = (SearchView) menu.findItem(R.id.search).getActionView();
mSrhView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSrhView.setSubmitButtonEnabled(true);
return true;
}
And the searchable item in menu:
<item android:id="#+id/search"
android:title="#string/srh_title"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
Any answer related is appreciated!
I solved the problem by changing this line:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchResultsActivity.class)));
Related
I'm following this Android tutorial to implement the Search View. After fixing some issues, I got it to work. However, the search view needs to be clicked twice to open the editText. Any idea of what's going on?
Filter class:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.toolbar_menu_filter, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_item_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.search_hint));
return super.onCreateOptionsMenu(menu);
}
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FilterUI">
<item android:id="#+id/menu_item_search"
android:title="Search"
android:icon="#drawable/ic_search_white"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<item android:id="#+id/menu_item_options"
android:icon="#drawable/ic_clear_all_white"
app:showAsAction="ifRoom"
android:title="#string/filter_default"/>
Manifest file
<activity android:name=".Activities.FilterUI"
android:screenOrientation="portrait"
android:configChanges="orientation"
android:windowSoftInputMode="adjustNothing">
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".Activities.SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Might be too late to answer but, let's get this done. Like I said, it's better to use:
app:actionViewClass="android.support.v7.widget.SearchView"
For more compatibility and etc. But the point is, in new AndroidX, we can't use android.support since we migrate to AndroidX and there will be only:
app:actionViewClass="android.widget.SearchView"
Available. But, let's see what documentation says for the newest APIs (AndroidX) and etc:
Note: This class is included in the support library for compatibility with API level 7 and higher. If you're developing your
app for API level 11 and higher only, you should instead use the
framework android.widget.SearchView class.
So in AndroidX, I have found:
app:actionViewClass="androidx.appcompat.widget.SearchView"
To use but you still can use: android.widget.SearchView.
After trying ALL of the solutions in stackoverflow about this problem, I decided to ask it here.
I'm trying to implement search bar in my Action bar.
I'm using AppCompatActivity and imported android.support.v7.widget.SearchView.
For the record, I'm using PageManager and TabLayout if it has any releation.
I get java.lang.NullPointerException error in the following line:
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
This is my code:
search bar declration at menu_main.xml:
<item
android:id="#+id/searchPlace"
android:title="#string/search_hint"
android:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="always"/>
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/search_hint" />
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" >
<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=".searchResultActivity" />
</activity>
<activity
android:name=".Settings"
android:label="#string/title_activity_settings" >
</activity>
<activity
android:name=".searchResultActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<!-- 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>
Search code in my MainActivity.java onCreateOptionsMenu:
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.searchPlace).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
Logcat error:
Process: il.co.test.test, PID: 1642
java.lang.NullPointerException
at il.co.test.test.MainActivity.onCreateOptionsMenu(MainActivity.java:106)
at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:267)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
UPDATE
I'm not sure, and I don't know how to check it - but I am pretty sure my getActionVIew() returns NULL.
UPDATE 2 - FIXED!
After checking some stackoverflow's about getActionView() null problem,
The 2nd answer at getActionView() of my MenuItem return null fixed my problem.
I had to use namespace app instaed of android.
Really painful!
Hope someone can find my mistake and help me fix it, since I tried many solutions for 6 hours.
Thanks in advance!
Search menu item
<item
android:id="#+id/searchPlace"
android:title="#string/search_hint"
android:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/search"
app:showAsAction="always|collapseActionView"/>
Activity code
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.searchPlace).getActionView();
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
private OnQueryTextListener queryTextListener = new OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String query) {
//if you need this
return false;
}
#Override
public boolean onQueryTextChange(String query) {
//your code
return true;
}
};
In this this case you dont need searchable configuration, dont need strokes in manifest, dont need SearchManager and other stuff. Just declare search item in the menu and thats it.
After checking some stackoverflow's about getActionView() null problem,
The 2nd answer at getActionView() of my MenuItem return null fixed my problem.
I had to use namespace app instaed of android.
Really painful!
My use case is the following: in activity A, I have an action bar with a collapsible SearchView. When the user gives her query and presses the 'search' button, I would like to show activity B with the results. I'm failing to do so, here is my code:
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/action_search_hint"
android:label="#string/app_name"/>
Activity A:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
AndroidManifest for Activity A and B:
<activity
android:name=".ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
Howerver, no matter what I try, getSearchableInfo in A always returns null.
I have the feeling I'm not understanding something. The only way I can make getSearchableInfo() return not null is when I put the SEARCH intent filter and the searchable meta-data into activity A in the manifest. But then, when I press the 'search' button, another instance of activity A is started again on top of the current one, which definitely is not what I want. I guess I could add singleTop to A and handle the SEARCH action in onNewIntent and start B, but I generally don't want A to be singleTop, I want the default. Or do I?
I tried adding this to A in the manifest (as suggested in some other threads):
<meta-data
android:name="android.app.default_searchable"
android:value=".ActivityB" >
</meta-data>
What am I doing wrong?
EDIT: I replaced the 'getComponentName()' line in A with the following:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));
and it seems to work the way I want! Am I doing something wrong here, or is this the correct way? I'm a bit uncertain because it is not mentioned anywhere!
According to Android documentations, SearchManager.getSearchableInfo(componentName) takes one argument, which is:
componentName: The activity to get searchable information for
In your case you need ActivityB, so constructing a ComponentName that points to ActivityB is the correct way
new ComponentName(this, ActivityB.class)
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
add this intent filter to activity A.
2.SearchView Menu
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|always"
android:title="#string/search_hint"/>
</menu>
3.Call this method from oncreate.
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Debug.e("Suggestion handle click", "call " + query);
startNextActivity(query);
}
}
4. if you are using Autosuggestion Listview implement searchView.setOnQueryTextListener ,setOnSuggestionListener and setSuggestionsAdapter.
I am new in android and I am working with a search related feature. After read the tutorial I have some questions:
1 Does the SearchableActivity unique in an application?
It seems that 3 steps are required to setup the search feature(from the android developer guide):
1)create the SearchableActivity to receive the search keyword.
2)make the SearchableActivity to accept the ACTION_SEARCH intent
3)specify the searchable configuration to use, in a element.
Then I wonder if all the search related job will be delegated to the SearchableActivity?
And suppose I have two MainActivity and MainActivityTwo, both of them make a SearchView inside the action bar.
The AndroidMinefset.xml:
<activity android:name="com.app.activity.MainActivityTwo" />
<activity android:name="com.app.activity.MainActivity" />
<activity android:name="com.app.activity.SearchableActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
xml/search.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" >
</searchable>
And register the search configuration(serchableInfo) toSerchView:
.MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.u_action_menu_map, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
This does not work, it seems that there is no serchableInfo bound with the current Activity(MainActivity).
Even I changed to this:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName("com.app.activity", "SearchableActivity")));
It still cannot find the searchableInfo.
After I add the search related configuration to MainActivity:
<activity android:name="com.app.activity.MainActivityTwo" />
<activity android:name="com.app.activity.SearchableActivity" />
<activity android:name="com.app.activity.MainActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
This worked. WTF.
I always thought that the Activity provide the Search UI and the Activity handle the input are different, but after my test it seems that they are the same Activity, that't to say the user input are submit to the Activity itself,but why use the **ing intent to transfer the input data?
2 The search submit
The Search dialog have a search submit button at the right of the dialog:
But there is nothing inside the SearchView so how to submit the search in a SearchView:
There should be a search key on your keyboard. It can also be go, return or similar keys that should start the search.
I'm having trouble setting up the search view in the action bar.
I followed the developer site, but it doesn't work. I get the feeling that I am missing something.
When I run my code, it always outputs "failed to get searchable info" (MainActivity.setupSearchView()).
MainActivity.java
// package...
// imports...
public class MainActivity extends Activity {
private static String tag = "Main Activity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.search_view, menu);
setupSearchView(menu);
return true;
}
private void setupSearchView(Menu menu) {
SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo si = sm.getSearchableInfo(getComponentName());
if (si == null) {
Log.wtf(tag, "failed to get searchable info");
return;
}
SearchView sv = (SearchView) menu.findItem(R.id.menu_search).getActionView();
sv.setSearchableInfo(si);
// Do not iconify the widget; expand it by default
sv.setIconifiedByDefault(false);
}
}
SearchResultsActivity.java
// package...
// imports...
public class SearchResultsActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent queryIntent = getIntent();
doSearchQuery(queryIntent);
}
#Override
public void onNewIntent(final Intent newIntent) {
super.onNewIntent(newIntent);
final Intent queryIntent = getIntent();
doSearchQuery(queryIntent);
}
}
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/search_label" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kpaek.examples"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".search.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=".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>
</manifest>
The key thing I found is that searchable.xml must not contain literal strings, only resources. I wasted about half a day on that one, since it causes getSearchableInfo to fail silently.
In my case using custom tags in res/menu/mymenu.xml worked for me:
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
Do not use:
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView"
I have found that the intent-filter and the meta-data elements must be included in the manifest activity element from which the search is initiated (MainActivity having a SearchView) for the method getSearchableInfo(getComponentName()) to return the correct configuration and not null.
<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>
I wonder if instead (and the solution I am going with for now) one should leave the intent and meta-data on the results activity declaration and change the argument to getSearchableInfo() as follows:
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
And the changes to MainActivity (note the assignment statement for searchableInfo):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView mySearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchableInfo searchableInfo = searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchResultsActivity.class));
mySearchView.setSearchableInfo(searchableInfo);
return true;
}
This killed me for a day - I thought it was ActionBarSherlock-related, but no it works fine with that.
The problem was that I was trying to short-circuit the sample. At least one of your activities - the one you're doing the searching from is sufficient - must have this intent-filter in it in the manifest:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
If not, then searchManager.getSearchableInfo(getComponentName()) always returns null, and your configuration is ignored.
From the developersite:
A searchable configuration defines how the SearchView behaves and is
defined in a res/xml/searchable.xml file. At a minimum, a searchable
configuration must contain an android:label attribute that has the
same value as the android:label attribute of the or
element in your Android manifest.
As an example, they show:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
app_name is, like in your manifest, the label of the activity. However, in your searchable, the label refers to search_label. Works perhaps if app_name and search_label are equal, but that seems fragile to me.
I encountered this issue but the advice above did not resolve it. Instead it turned out to be setting category default in the SearchResultActivity. For example the following breaks:
<activity android:name=".SearchResultActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<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>
Removing android.intent.category.DEFAULT resolves this problem.:
<activity android:name=".SearchResultActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Also the previous answers to this question have an error. MainActivity should be defined with android.app.default_searchable. android.app.searchable is only for your SearchResultsActivity.
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<!-- other stuff-->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
In conclusion the SearchManager is a fragile component prone to silent failures when the AndroidManifest.xml isn't perfect.
I wasted 8 full hours on this one because I checked all the above solutions but one thing was missing for me.
In my Android Manifest:
<meta-data
android:name="android.app.searchable"
android:value="#xml/searchable" />
Should has been:
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
For me it was failing silently. The best thing to do at first is just copy and paste the examples. Just be sure that all your attributes are exactly the same.