Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

SearchView setQuery, other customizations not possible? #1029

Open
inmyth opened this issue Sep 2, 2013 · 4 comments
Open

SearchView setQuery, other customizations not possible? #1029

inmyth opened this issue Sep 2, 2013 · 4 comments

Comments

@inmyth
Copy link

inmyth commented Sep 2, 2013

I picked the code from the demo SearchViews below and added a single line setQuery to searchView in onCreateOptionsMenu method. It didn't work. In fact I see that at least setIconified doesn't work either. Is this behavior expected from creating searchView on onCreateOptionsMenu method ? Because the only way I could make it work is by adding the modified searchView to ActionBar.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
    final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());            
   searchView.setQuery("test", false);

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}
@anshulupadhyay03
Copy link

I am also having same issue.I am trying to setQuery("test",false); in a method but i dont see the text visible in the searchView.Please fix this.

@551780457
Copy link

I am also having this issue......are there anyone can help to fix it....

@dandar3
Copy link

dandar3 commented Dec 24, 2013

Your problem is caused by the fact that when you click to expand the SearchView it will call SearchView.onActionViewExpanded() where it sets the text to blank (if you put a breakpoint and debug the code you'll see it):

1289 mQueryTextView.setText("");
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/src/com/actionbarsherlock/widget/SearchView.java#L1289

Maybe it could set it to mUserQuery instead, I don't know...

If what you want to is to default a search value for when the user expands the search view, you could try overriding the OnActionExpandListener, e.g.:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        [...]

        // Search...
        MenuItem searchMenuItem = menu.add("Search");
        searchMenuItem.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
                      .setActionView(searchView)
                      .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem p_menuItem) {
                SearchView searchView = (SearchView) p_menuItem.getActionView();
                searchView.onActionViewExpanded();
                searchView.setQuery("TEST", true);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem p_menuItem) {
                return true;
            }
        });

        return true;
    }

You may notice that inside onMenuItemActionExpand() I am calling searchView.onActionViewExpanded() - if you don't call it in there, it will get called afterwards and it will blank your search value again (again, found by putting a breakpoint in ABS SearchView code).

HTH,
Dan

@igtsekov
Copy link

igtsekov commented Dec 7, 2015

This save me thank you :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants