Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView and Activity Title in ActionBar

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: Has the activity title.

After expanding: So much free space I could rent it.

(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?

like image 928
user1987392 Avatar asked Nov 26 '13 11:11

user1987392


People also ask

How do I customize my SearchView widget?

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.

What is the difference between action bar and toolbar?

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.


1 Answers

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;
        }
    }
});
like image 189
thisbytes Avatar answered Sep 29 '22 18:09

thisbytes