android want to add searchview in actionbar but app crash - android

i am working on action bar . i want to add searchview option on the action bar but app crash here.
this is crash
this is my code of option menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>
this is the code of searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/enter"
android:includeInGlobalSearch="false"
android:label="#string/search"
android:searchSettingsDescription="#string/search_global_description" />
and this is my activity code
public class Thrd extends ActionBarActivity {
Menu m;
final Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thrd);
getSupportActionBar().setTitle("3rd page");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#20a780"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.options_menu, menu );
// Add SearchWidget.
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem( R.id.search ).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );
return super.onCreateOptionsMenu( menu );
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case R.id.action_Exit:
openExit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openExit() {
}
}
please help me to solve my problem.thanks advance

This was how I implemented my search handling.
In the XML folder under layout, add a searchable.xml file and put this code like so:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search Trends" />
Then in your ANDROID manifest file, add
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.tobisoft.trendify.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
//This is the meta to add for your activity
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
Also you would need to make an activity for the search results like so:
<activity
android:name=".SearchResultsActivity"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<!-- 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>
now in the menu folder, with a file name main.xml (or anything you are using)
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:orderInCategory="100"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
You can get the icon yourself by a simple google search. You would also need to add the AppCompat Library to your project, something you can also do from a simple google search. In the SearchResultsActivity, add this:
public class SearchResultsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, 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;
}
#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
}
}
}
The activity_result.xml layout file is this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff141414">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="#dimen/_25sdp"
android:textStyle="bold"
android:text="Search Result goes here!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In your MainActivity.java add this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, 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;
}
All this shld help your app on the right track. Hope it helps

Changing to import android.support.v7.widget.SearchView helped me and also change these lines
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView= (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);`

Related

SearchView not working

I have a SearchView implementation that's not working at all. I have tried a lot of things, but nothing is working.(this,this and other answers)
What am I doing wrong? I am trying to log the partial results or something that tells me it is working, but I don't get anything.
Manifest.xml
<activity
android:name=".MapaActivity"
android:label="#string/title_activity_mapa"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
</activity>
menu.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.buweb.bu.MapaActivity">
<item android:id="#+id/search"
android:title="#string/menu_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Activity
public class MapaActivity extends BaseActivity implements OnMapReadyCallback, SearchView.OnQueryTextListener {
... //A lot of code doing stuff with map.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mapa, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("", "query:" + query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d("", "query:" + newText);
return false;
}
You are missing some implementations:
1-Create a Searchable Configuration:
A searchable configuration defines how the SearchView behaves and is defined in a res/xml/searchable.xml file.
<?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" />
2- In your application's manifest file, declare a <meta-data> element that points to the res/xml/searchable.xml file, so that your application knows where to find it. Declare the element in an <activity> that you want to display the SearchView in:
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
3- In the onCreateOptionsMenu() method that you created before, associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo):
#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(getComponentName()));
return true;
}
For further information you can refer to the following links:
https://developer.android.com/training/search/setup.html#add-sv
https://developer.android.com/guide/topics/search/search-dialog.html#SearchableActivity
How to implement a Searchview in android?
How to use SearchView in Toolbar Android
Implementing SearchView in action bar

Search on ActionBar

Level:Beginner
I am trying to implement a search bar on ActionBar. I want that when someone clicks the search icon, he should get a text field, where he simply fills in the text and can search by submitting through the keyboard search Button. Following several tutorials, I am quite confused.
When I do not add
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
when I add
`searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); `
I get an error 'unfortunately, Application has stopped' and the logcat shows:-
Process: com.example.bhavya.myapplication, PID: 23261
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setIconifiedByDefault(boolean)' on a null object reference
at com.example.bhavya.myapplication.MainActivity.onCreateOptionsMenu(MainActivity.java:27)
#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_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
I also tried to set 'intent-filter' and 'meta-data' in the following way after I was unsuccesful
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bhavya.myapplication" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".SearchableActivity"/>
</activity>
<activity
android:name=".SearchableActivity"
android:label="#string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
but nothing changed, I also made a folder "xml" and set searchable configuration
<?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>
This is my menu.
<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_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is my 2nd activity
public class SearchableActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, 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;I get an icon for search, but it does not expands into a text field. So I am not able to search anything from there.
}
#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
}
}
}
I tried to look up a lot, but only got confused more. Please Help.

Android - SearchView in toolbar not expanding

I'm trying to add a SearchView in my toolbar, I've added the icon, but when I press it, it's not expanding.
Here's the code:
main_menu.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.app.try.MainActivity">
<item
android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager= (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView=null;
if (searchItem!=null) {
Log.d("createOptionMenu","search item not null");
searchView = (SearchView) searchItem.getActionView();
}
if (searchView!=null) {
Log.d("createOptionMenu","search view not null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}else{
Log.e("createOptionMenu","search view null");
}
return true;
}
(this always shows the log message "search view null")
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".SearchResultActivity"
android:label="#string/title_activity_search_result" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
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="HINTTT" />
I've tried multiple options as using the expandActionView() or the setIconified(false), but non of those works for me.
You should use MenuItemCompat
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
and for collapsing SearchView use
MenuItemCompat.collapseActionView(searchItem);
and make sure you have changed android:actionViewClass to app:actionViewClass
I had same problem. I just solved mine by making a minor tweak. change
android:actionViewClass="android.support.v7.widget.SearchView"
to app:actionViewClass="android.support.v7.widget.SearchView"
as the docs say, you have to set the collapseActionView (bitwise-OR it with |ifRoom) flag for the showAsAction attribute in the menu.xml file.
Now both exapandActionView() and collapseActionView() will work.
In MainActivity you have to extends ActionbarActivity
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
MainActivity.java
public class MainActivity extends ActionBarActivity {
#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_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}

Start new activity from SearchView

I have 2 activities: the first has a action bar with a search view, the second should display the results of the search query.
androidmanifest:
<activity
android:name=".SearchActivity"
...
android:launchMode="singleTop">
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
...
</activity>
<activity
android:name=".ResultsActivity"
...
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
searchable.xml
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/enter_a_word" />
SearchActivity
....
#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_noun_list, 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;
}
....
ResultsActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
}
...
}
the problem is that after a query is entered into the searchview, nothing happens. No errors, nothing. How can i open the resultsactivity after the query is entered in the searchactivity?
This answer is a little late but I feel it'll be useful for future viewers. The dilemma seems to come from the ambiguity of the Android SearchView tutorial. The scenario they cover assumes you will be displaying the results in the same Activity the SearchView resides. In such a scenario, the Activity tag in the AndroidManifest.xml file would look something like this:
<activity
android:name=".MainActivity"
android:label="#string/main_activity_label"
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>
Then, to handle the results in the same Activity, you would Override the onNewIntent method:
#Override
public void onNewIntent(Intent intent){
setIntent(intent);
if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//now you can display the results
}
}
However, in a situation where we want to display the results in another Activity, we must put the Intent Filter and meta tag into the results Activity and introduce a new meta tag for the SearchView Activity. So, our Activities will look something like this in the AndroidManifest.xml file:
<activity
android:name=".MainActivity"
android:label="#string/main_activity_label"
android:launchMode="singleTop">
<!-- meta tag points to the activity which displays the results -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="#string/results_activity_label"
android:parentActivityName="com.example.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
<!-- meta tag and intent filter go into results activity -->
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
Then, in our MainActivity's onCreateOptionsMenu method, activate the SearchView (assumes you're adding the SearchView to the ActionBar). Rather than using getComponentName() in the SearchManager's getSearchableInfo() method call, we instantiate a new ComponentName object using the MainActivity's context and the SearchResultsActivity class:
#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_home, menu);
SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
search.setQueryHint(getResources().getString(R.string.search_hint));
return true;
}
Finally, in our SearchResultsActivity class, in the onCreate method, we can handle the search results:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
Don't forget to create the searchable.xml resource file and add the SearchView to your layout.
searchable.xml (res/xml/searchable.xml; create xml folder under res if needed):
<?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"/>
Layout (example of adding the SearchView to ActionBar as a menu item):
<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.example.MainActivity">
<group android:checkableBehavior="single">
<item android:id="#+id/action_search" android:title="Search"
android:orderInCategory="1" app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</group>
</menu>
Resources:
Display Results In Same Activity
Display Results In Different Activity
ComponentName
I understood the problem i had also faced the same, This is happening because you are passing the current component name by passing the
getComponentName()
This will be initialize by the current activity name so you need to initialize it with the searchable activity name in given below format and pass the same Component instance it starts the new activity.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
search.setQueryHint(getResources().getString(R.string.search_hint));
Hope it I have answered the Question!
Without seeing your activity code, I would suggest you try this approach - also assuming you have all the files created as stated above;
In your results activity,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.core_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(this);
return true;
}
Remember that this is the activity that has your data that you want to make searchable:
You must implement the SearchView.OnQueryTextListener interface in the same activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
ProductsResulstActivity.this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
productFilterAdapter.getFilter().filter(newText);
if (TextUtils.isEmpty(newText)) {
listView.clearTextFilter();
}
else {
listView.setFilterText(newText);
}
return true;
}
productFilterAdapter is the adapter that you must create beforehand.
It should implement Filterable interface. I hope this helps.
If you need further assistance, please let me know. Good luck

SearchWidget does nothing

Even after reading the samples and a few questions on SO, I still dont figure out why my searchwidget does nothing ... !
Manifest.xml simplified:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".ResultActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
</activity>
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/search_hint" >
</searchable>
menu/activity_main.xml (searchwidget in action bar..) :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
MainActivity where the search is supposed to happen:
public class MainActivity extends Activity implements OnClickListener {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setSubmitButtonEnabled(true);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// app icon in action bar clicked; go parameters
Intent intent = new Intent(this, ParametersActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
case R.id.menu_search:
onSearchRequested();
return true;
}
return true;
}
}
and Resultactivity :
public class ResultActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
Toast.makeText(this, "QUERY : " + query, Toast.LENGTH_SHORT).show();
}
}
I've followed API guide, and I dont see where is my mistake, if anyone can help, I'd appreciate !!
Thanks.
Nico.

Categories

Resources