ActionBar SearchView not fully expanding in landscape mode

10,636

Solution 1

Did you try something like that?

editView.setOnKeyListener(new OnKeyListener() {
    private boolean extended = false;

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!extended && event.getAction() == KeyEvent.ACTION_DOWN) {
            extended = true;
            LayoutParams lp = v.getLayoutParams();
            lp.width = LayoutParams.MATCH_PARENT;
        }

        return false;
    }

});

Solution 2

MenuItemCompat's SearchView has a property named maxWidth.

    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setMaxWidth(xxx);

use screen width instead of xxx offcourse

Share:
10,636
Collin Buchan
Author by

Collin Buchan

Updated on June 09, 2022

Comments

  • Collin Buchan
    Collin Buchan almost 2 years

    When using the action bar search interface, the widget expands to occupy the full width of the screen in portrait mode, but stops short in landscape mode.

    Is there a way to set the expansion layout params on the SearchView to fully fill the action bar when the user is typing a search?

    See image:

    SearchView text field not using the full width in landscape mode

    Note: I'm not currently using ActionBar Sherlock

    Edit: Here's what I did to get it to extend the full width.

    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;
            }
        }
    
    });
    
  • Collin Buchan
    Collin Buchan about 11 years
    Close. I wound up needing to use setOnSearchClickListener() since I'm not going for hardware key press search.
  • Darklex
    Darklex over 9 years
    How smooth is this to achieve in the ActionBar?
  • Anton Savenok
    Anton Savenok about 8 years
    searchView.setMaxWidth( Integer.MAX_VALUE );