6

I am using a SearchView in android. When I first land on the page, all that is visible is the search icon. Then when I click on the icon, the EditText appears. How do I get the EditText to be visible from the get go? Here is my xml:

<SearchView
        android:id="@+id/search"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:queryHint="@string/search_hint"
        android:textSize="16sp" />
user3093402
  • 1,419
  • 4
  • 20
  • 28

4 Answers4

3

EDIT---

Oh sorry! I tought you wanted to do an 'empty' query. I think you can open SearchView programmatically using:

searchView.setIconified(false);

OLD ANSWER--

Inside an Activity you can try with:

onSearchRequested();

And inside a Fragment you can try with:

getActivity().onSearchRequested();

Good luck!

pozuelog
  • 1,284
  • 13
  • 27
  • thanks I came here to post my answer: `android:iconifiedByDefault="false"`. You got it first. Thanks. – user3093402 Jan 07 '14 at 19:09
  • do you know the answer to this follow up question? http://stackoverflow.com/questions/20981613/setting-iconified-to-false-on-android-searchview-cause-view-to-not-be-searchable – user3093402 Jan 07 '14 at 20:51
1

From the official Android docs:

If you want the search field to always be visible, then call setIconifiedByDefault(false).

This does work for the compatibility android.support.v7.widget.SearchView as well.

theblang
  • 10,215
  • 9
  • 69
  • 120
1

You can try this:

SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Expand the search view
searchItem.expandActionView();

Thanks.

user5071787
  • 413
  • 1
  • 4
  • 10
0

You also have to set this property for click event

searchView.setIconified(false);
        searchCloseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchEditText.setText("");
                ((SearchView) findViewById(R.id.search_view)).setIconified(false);
            }
        });
Asad Mehmood
  • 341
  • 2
  • 12