android search widget menu item - android

I use this code as this Android API Guides suggest to have a search widget in android 3.0 or more:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, 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
return true;
}
I have declared search menu item (and other menu item) in res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:icon="#android:drawable/ic_menu_help"
android:title="Help"
/>
<item android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
android:showAsAction="ifRoom"
/>
</menu>
I also created an activity for search results and its res/xml/searchable.xml settings, as API Guides suggest.
But as I start my app i get a NullPointerException atonCreateOptionsMenuand at onCreatePanelMenu

As #A--C said, You forgot that your menu_search item needs android:actionViewClass="android.widget.SearchView
Try adding that and see if it works. If not, make sure to attach your LogCat stack trace as an edit to your question.

Related

SearchView widget isn't showing up on the ActionBar

I have been trying to put a SearchView widget on the Action bar, but it isn't showing up. I have tried the tutorial from google http://developer.android.com/training/search/setup.html but nothing happens to the Action bar. Here is the code for further reference.
options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is the code that I wrote in the Jave file of the Activity I want the SearchView widget in.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
Minimum sdk = 14
Please Help!

Search Actionview not collapsing in single up button click android actionbar compat

I am using actionbar compat support library to provide search widget in Action Bar. The search action view is collapsing only after pressing back button or up button twice. I had same problem while executing the sample dictionary search also.
Code in my search activity which extends ActionBarActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
// Find the search item
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
My Searchable.xml looks like this
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search_label"
android:hint="#string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
>
Let me know if any more information needed...
Check the API guide http://developer.android.com/guide/topics/ui/actionbar.html#ActionView This is what it should look for anything less than API 11.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:orderInCategory="9"
yourapp:menuCategory="system"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="android.widget.SearchView"
android:title="#string/action_searchSt" />
</menu>
That's probably more attributes than you need (menuCategory and orderInCategory are unnecessary). But assuming you've added in the support library correctly and you read the link I gave, things should work as intended.

how to send information in search bar

i tried to put a searchbar into my app and i got it so far to type in what i wanna search, but how do i "send" that given information?
The problem is i can type something in, but theres no button to start the search and i dont have much experience in app building...
the menu:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.klassenliste, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
xml files
searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
and a menu file:
klassenliste.xml
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_btn_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"></item>
The actual "sending" of the search query is done with the assistance of the Android system itself. That said, you still need to specify which activity to launch to process and display the search query.
Assume that SearchActivity is your target activity for showing/processing search queries. See my answer here. Also, your onCreateOptionsMenu(Menu menu) method seems to be missing important lines of code. Refer the docs. Use a SearchView if developing for Android 3.0+ and using an ActionBar.

SearchView taking all the space in the new ActionBarCompat

I switched from ActionBarSherlock to ActionBarCompat (support library v7). After some adjustments, almost everything is working fine by now.
But I'm in trouble with the SearchView in the ActionBar. When it's expanded (actually, It's always expanded in my Activity), it takes up all the space and doesn't respect the space of other Action Items that are set to show always (showAsAction="always").
To simulate the problem, use this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_buscar"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always"
android:title="#string/buscar"/>
<item android:id="#+id/tentar_novamente"
android:title="#string/tentar_novamente"
android:icon="#drawable/acao_tentar_novamente"
app:showAsAction="always" />
</menu>
In the Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.busca_action_menu, menu);
searchMenuItem = menu.findItem(R.id.menu_buscar);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconifiedByDefault(false);
searchView.setQueryHint(stringBusqueArtistasMusicasEAlbuns);
return super.onCreateOptionsMenu(menu);
}
This is the result in the android 4.3:
And this is the result in the android 2.3 (the action items doesn't even appear):
The same problem happens when I use:
setSupportProgressBarIndeterminateVisibility(true);
The progress indicator appears very strange in the Android 4.3 and doesn't appear in Android 2.3.
The same code worked as expected with ActionBarSherlock. When there was some action item, the SearchView used to decrease its width to give space for the action items.
UPDATE:
I've posted an issue in the Android's Bug Tracker:
https://code.google.com/p/android/issues/detail?id=58251&thanks=58251&ts=1375191632
If you have the same problem, please follow the issue.
UPDATE:
I've tried to put the action items before the SearchView and this way they items appear.
But, the setSupportProgressBarIndeterminateVisibility(true) is still not showing the progress bar.
This is definitely a bug about Android but a workaround can be including SearchView programmatically like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(false);
getActionBar().setCustomView(searchView);
getActionBar().setDisplayShowCustomEnabled(true);
}
You can also use a layout XML to define SearchView properties.
However "iconifiedByDefault" in XML tends to be ineffective in my experience. (This may my bad though)
Thanks for creating an issue about this.
Here's the URL to the related bug report:
https://code.google.com/p/android/issues/detail?id=58251
Despite what is mentioned in the bug report, my experience was the same with both ActionBarSherlock and ActionBarCompat. So I expect that ActionBarSherlock users are also affected.
Have you tried using collapseActionView()?
I use it like this:
public static MenuItem msearchMenuItem;
#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();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
msearchMenuItem = menu.findItem(R.id.search);
return true;
}
public static MenuItem getSearchMenuItem() {
return msearchMenuItem;
}
public void doSomething(){
//Collapse the SearchBar
getSearchMenuItem().collapseActionView();
}
I don't know if it works with v7, but it certainly works with v4.
Try changing android:showAsAction="collapseActionView|ifRoom"
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
Remove the line
searchView.setIconifiedByDefault(false);
Or you can explicitly call the method with true as the argument.

Android 3.0 honeycomb search view on action bar

How to have a 'search item' on the action bar for honeycomb? If possible please provide full code with layout.
I'm assuming you mean adding the search widget to the actionbar?
Here's how it's done:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is of course placed in the res/menu folder.
You add this to your actionbar just like any adding any other menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Set appropriate listeners for searchView
...
return super.onCreateOptionsMenu(menu);
}
This was all taken from here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Categories

Resources