Very simple question that doesn't need much code:
I'm using Android's default ActionBar (no Sherlock) in which I have a couple of MenuItems. One of them is a collapsible Action View (android:showAsAction="collapseActionView|always"
) for your typical search scenario: user clicks on the icon → a search box expands → when the user types anything onQueryTextChange does its magic).
SearchView sv = new SearchView(this);
sv.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
// Yada, yada...
sv.setOnQueryTextListener(new OnQueryTextListener() {
// The methods for onQueryTextSubmit and onQueryTextChange ... they apply filtering on the activity's list adapter and apparently work fine
}
menuItem.setActionView(sv); // menuItem is the item for the search action
searchAnswers.setOnActionExpandListener(new OnActionExpandListener() {
// The listener's methods for expand/collapse as I need some business logic behind them
}
All of the above works fine.
When the device is on portrait mode and has limited screen space, the SearchView occupies pretty much the whole action bar, hiding the other MenuItems (which have android:showAsAction="ifRoom"
) as well as the Activity's title, which is OK by me.
The problem is that if the device is on landscape mode (so that there's still free ActionBar space), the SearchView doesn't occupy the whole ActionBar (again, fine by me) but the Activity's title disappears (even though there's plenty of space where it could be displayed!). So my problem is that I get this empty space where the Activity's title could be shown.
Before expanding:
After expanding:
(Both screenshots are from a phone in landscape mode. Tested with Android 4.0.4 and 4.3.)
Any tips on how to keep the Activity's title when there's enough space?
just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.
The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.
Not sure if you found an answer to this problem yet. But I found an answer on this thread that may help - it certainly helped me.
ActionBar SearchView not fully expanding in landscape mode
Code copied from link:
searchView.setOnSearchClickListener(new OnClickListener() {
private boolean extended = false;
@Override
public void onClick(View v) {
if (!extended) {
extended = true;
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
}
}
});
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