Collin Buchan
Collin Buchan

Reputation: 267

ActionBar SearchView not fully expanding in landscape mode

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

});

Upvotes: 11

Views: 10872

Answers (6)

Wops
Wops

Reputation: 993

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

Upvotes: 17

gc986
gc986

Reputation: 756

Use this (100%):

mSearchView = (SearchView) searchItem.getActionView();    
mSearchView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Upvotes: 0

Fher Ayala Castro
Fher Ayala Castro

Reputation: 29

For ActionBarCompat use this:

    MenuItemCompat.setShowAsAction(searchItem, MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
            | MenuItem.SHOW_AS_ACTION_ALWAYS);
    MenuItemCompat.expandActionView(searchItem);

Upvotes: 0

Etienne Lawlor
Etienne Lawlor

Reputation: 6687

Adjust the width layout param to MATCH_PARENT, that way the SearchView will fully expand in either portrait or landscape

ViewGroup.LayoutParams layoutParams = mSearchView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

Upvotes: 0

crina
crina

Reputation: 345

I figured out a solution for fully expanding the search view in landscape and to also have the action view already expanded when the activity is created. Here how it works:

1.First create an xml file in your res-menu folder called for example : searchview_in_menu.xml. Here you would have the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:id="@+id/action_search"
              android:title="@string/search"
              android:icon="@android:drawable/ic_menu_search"
              android:actionLayout="@layout/searchview_layout" />
    </menu>

Note: "@string/search" - looks something like this in the res-strings.xml:

    <string name="search">Search</string>

2.Second create the layout referred above ("@layout/searchview_layout") in res-layout folder. The new layout: searchview_layout.xml will look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <SearchView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/search_view_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

Note: Here we are setting the search view width to match the width of its parent( android:layout_width="match_parent")

3.In your MainActivity class or in the activity that has to implement the Search View write in the onCreateOptionsMenu() method the following code:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        //find the search view item and inflate it in the menu layout
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        //set a hint on the search view (optional)
        mSearchView.setQueryHint(getString(R.string.search));
        //these flags together with the search view layout expand the search view in the landscape mode
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                | MenuItem.SHOW_AS_ACTION_ALWAYS);
        //expand the search view when entering the activity(optional)
        searchItem.expandActionView();
        return true;
}

Upvotes: 0

cyanide
cyanide

Reputation: 3964

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

});

Upvotes: 4

Related Questions