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.
Last modified on March 26th, 2015 by Joe. This Android tutorial is to learn about using a SearchView in action bar of an Android app. There are two main things that I will handle in this tutorial. Incorporating a Search component in the Android action bar is a common use which we come quite often.
To use the appcompat-v7 support library, we should add the library in Gradle dependency. This is for people using Android Studio. For people using Eclipse+ADT, I recommend you to migrate to Android Studio as soon as possible.
You will see an unwanted space is added at the bottom of the CollectionView with MistyRose color The CollectionView height should end when its child items height ends. The CollectionView vertically expands unnecessary even after its child elements which are populated in its ItemTemplate was ended.
The CollectionView height should end when its child items height ends. The CollectionView vertically expands unnecessary even after its child elements which are populated in its ItemTemplate was ended. Version with issue: Xamarin.Forms 4.1.0.483098-pre1
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With