SearchView taking all the space in the new ActionBarCompat - android

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.

Related

SearchView not getting focus

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))
}
}

SearchView using AppCompat

I implemented SearchView in Actionbar before using appcompat.v7
but when I want to use the SearchView with support library v7 it shows null exception
In style
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" />
In Java Class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
My question is how to declare SearchView in onCreateOptionsMenu to be able to set query listener?
You should use the static methods in MenuItemCompat do deal with all AppCompat menu items. This was mentioned in this blog post as the last item under 'New Integration'. Just replace your SearchView declaration with the following.
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Here's a link to the MenuItemCompat documentation.
Edit:
I just assumed you are using the latest version of AppCompat with the support for the new Toolbar widget.
If you change android:actionViewClass to app:actionViewClass your existing code will continue working.

Action View in android l

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.

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.

How to keep expanded SearchView on the right side of ActionBar (while using custom icon)?

My question is closely related to SearchView wrong alignment and SearchView positions when expanded in ActionBar but the answers posted there do not work.
So, I'm using a SearchView in the Action Bar:
<item android:id="#+id/action_search"
android:icon="#drawable/navi_search"
android:title="#string/action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
It looks like this by default (top-right corner, next to overflow menu):
Problem is, when expanded, I'd like the SearchView to stay on the right, but instead, it moves to the left edge, next to home button (app icon).
I tried changing showAsAction to either of these (as suggested here):
android:showAsAction="ifRoom"
android:showAsAction="always"
...And that does fix the alignment when expanded, but the custom icon is lost when in collapsed state:
Also, stuff like android:layout_alignParentRight="true" and android:layout_centerInParent="true" on the menu <item> have no effect.
Any ideas?
I'm targetting Android 4.0 and above:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
Alright, I found a way:
In onCreateOptionsMenu(), after the standard menu inflating stuff, set
ActionBar.LayoutParams with Gravity.RIGHT for the SearchView instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
// ...
// Find the SearchView; make it aligned to right, also in expanded mode:
MenuItem item = menu.findItem(R.id.action_search);
SearchView search = (SearchView) item.getActionView();
search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
}
(In the menu definition XML, I still use android:showAsAction="ifRoom|collapseActionView".)
Now the SearchView opens on the right, in the same place where the icon is, and the custom icon is still in use.
Try editing this line
android:showAsAction="ifRoom|collapseActionView"
to:
android:showAsAction="ifRoom"
it will align the edittext to the right.
Using this worked for me:
final SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnSearchClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
searchView.setLayoutParams(new Toolbar.LayoutParams(Gravity.END));
}
});

Categories

Resources