I have an application with a search widget in the action bar. The application is not creating the search widget as an actionview when I switch the theme to use Material.Light in android L. When debugging I can see that the search view is null.
The menu item:
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/title"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
The creation of the menu:
Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.homescreen, menu);
return true;
}
The initialization of a variable for the search view:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnQueryTextListener(queryListener);
return super.onPrepareOptionsMenu(menu);
}
Any ideas?
Update 1:
Ok so I stopped the app from crashing by replacing app:actionViewClass="android.widget.SearchView" with android:actionViewClass="android.widget.SearchView". But now the search item shows up in the overflow menu rather than the action bar even though it is set to show as action always.
I had this same problem with setting it to show "always" and it ended up always being in the overflow. If you are using the support library make sure you are extending ActionBarActivity, if not, the regular Activity should work.
Related
EDIT For anyone wondering, my problem was I was using android.widget.SearchView instead of android.support.v7.widget.SearchView. I hope this helps anyone else with the same problem!
Original Post
I'm trying to implement SearchView in the Android ActionBar as per the official guide: http://developer.android.com/training/search/setup.html
After failing to find the problem, I finally stripped down to the most basic Hello World application and found to my surprise that the bug still persists in a minimal app!
Here's the bug:
The search icon appears in the menu bar, no problem. When I click it, the search bar expands (as expected) but there is no cursor and no soft keyboard appears. (I want to post a picture but my reputation is too low :(
Here is the relevant code, although I literally just created a new Android Application and added the item to menu/menu_main.xml.
MainActivity.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.menu_main, menu);
return true;
}
menu_main.xml
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#android:string/search_go"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView" />
use this is 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="your activity context here"><item
android:id="#+id/mi_search"
android:title="#string/search"
android:orderInCategory="2"
android:icon="#drawable/searchicon"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
and this code in onCreateOptionsMenu(Menu menu):
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);
MenuItem searchItem = menu.findItem(R.id.mi_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
in onOptionsItemSelected():
case R.id.mi_search:
onSearchRequested();
break;
Here in 2022 this question still applies but the answer must be updated if you use androidx (which you should).
The menu item must use androidx.appcompat.widget.SearchView instead of either android.widget.SearchView or android.support.v7.widget.SearchView.
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#android:string/search_go"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
The onCreateOptionmenu override must also be updated to use the androidx class. Here is the Kotlin code for that:
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main, menu)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
menu.findItem(R.id.search).actionView as androidx.appcompat.widget.SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
}
}
I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
1) Called the setHasOptionsMenu(true); in every fragment.
2) Added the onCreateOptionsMenu() in every fragment. and
then i have achieved what i want.
Now i want that onClick of the searchview it should get expanded and collapse when pressed the back. So to achieve that i have added this in my first 3 fragment and also implement the puru's logic .(Create the onPrepareOptionMenu() in every fragment and add the onQueryTextChange(""); method).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().supportInvalidateOptionsMenu();
setHasOptionsMenu(true);
}
Fragment Code ::
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search for Products");
searchView.setOnQueryTextListener(this);
searchView.setIconifiedByDefault(false);
}
#Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
onQueryTextChange("");
}
#Override
public boolean onQueryTextSubmit(String query)
{
return true;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_search"
android:icon="#drawable/abs__ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:title="Search Products">
</item>
<item
android:id="#+id/root_menu"
android:icon="#drawable/abs__ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/menu_Home"
android:icon="#drawable/home"
android:showAsAction="never"
android:title="Home"/>
<item
android:id="#+id/menu_favourite"
android:icon="#drawable/favourite"
android:showAsAction="never"
android:title="Favourite"/>
<item
android:id="#+id/menu_Balance"
android:icon="#drawable/balance"
android:showAsAction="never"
android:title="Balance"/>
<item
android:id="#+id/menu_logout"
android:icon="#drawable/btn_logout"
android:showAsAction="never"
android:title="Logout"/>
</menu>
</item>
</menu>
Problem :: (on Pre-HoneyComb Device)
Now when i click of the first fragment's searchview,it not get expanded,but works well for fragment 2 and fragment 3.
Steps ::
1)go to directly fragment 1,click on searchview,not expanding
2)go to directly on fragment 2 tab(not clicking onsearchview in second fragment),back to fragment 1,searchview expanding
3)go to directly on fragment 3 tab(not clicking onsearchview in third fragment),back to fragment 1,searchview expanding
Is there any issue related to the focus of searchview? or am i calling the supportInvalidateOptionsMenu() in some unusual way?
Let me know if you need anything more from my side...
Please guide me in my implementation... Any clue why this strange behaviour happens? Any suggestion/explnation will be appreciated....
Plz plz help me on this issue....
Thanks in Advance...
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.
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.
I am able to expand search view by action like this
<item android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="never|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
But i have a 3-tab activity and i'd like to SearchView be always expanded
How may I do that?
Two steps are necessary.
First, you have to make sure your search menu item is always shown as an action and never moved into the overflow menu. To achieve this set the search menu item's showAsAction attribute to always:
<item
android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
Second, make sure the action view is not shown in iconified (i.e. collapsed) mode by default. To do this call setIconifiedByDefault(false) on your search view instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_activity, menu);
MenuItem searchViewItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
[...]
searchView.setIconifiedByDefault(false);
return true;
}
That should do it.