user1987392
user1987392

Reputation: 3991

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?

Upvotes: 6

Views: 1688

Answers (3)

Michele Dorigatti
Michele Dorigatti

Reputation: 817

Building on CoolMind answer, it worked for me to set max width to a smaller value:

searchView.setMaxWidth(375);

setMaxWidth() interprets its parameter as pixels.

Upvotes: 0

CoolMind
CoolMind

Reputation: 28793

searchView.setMaxWidth(Integer.MAX_VALUE);

Upvotes: 0

thisbytes
thisbytes

Reputation: 817

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

Upvotes: 1

Related Questions