ruruma
ruruma

Reputation: 603

How to make SearchView always expanded in android?

I have SearchView in the top of the layout (not in the action bar), is there anyway to force this View to be always expanded (opened)?

If not, i wish to place fancy image near it, is there anyway to make SearchView hide this image when expanded (clicked/expanded)?

Upvotes: 49

Views: 34779

Answers (4)

yoAlex5
yoAlex5

Reputation: 34205

You can try to use

searchView.onActionViewExpanded();

and if you do not need keyboard opened

searchView.clearFocus();

Upvotes: 5

dharmx
dharmx

Reputation: 1174

I am doing this into fragment , i done it by using

searchItem.expandActionView();

but when user press back button it collapse so in collapse method i used

getActivity().getSupportFragmentManager().popBackStack();
// when it collapsed i am going back

Below is my solution in detail:

search_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search_contacts"
        android:title="search"
        android:icon="@drawable/search2"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" >
    </item>
</menu>

in fragment i used below code :

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.search_menu, menu);

        // Define the listener
        MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item)
            {
                getActivity().getSupportFragmentManager().popBackStack();
                // Do something when action item collapses
                return true;  // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item)
            {

                // Do something when expanded
                return true;  // Return true to expand action view
            }
        };



        MenuItem searchItem = menu.findItem(R.id.search_contacts);


        // Assign the listener to that action item
        MenuItemCompat.setOnActionExpandListener(searchItem, expandListener);

        // Any other things you have to do when creating the options menu…



        SearchView searchView =
                (SearchView) MenuItemCompat.getActionView(searchItem);
        //searchView.setIconifiedByDefault(false);
        searchItem.expandActionView(); // when fragment opens it expanded auto


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText)
            {
                seachViewFunction(newText); // in searchViewFunction(newText); i am doing my things
                return false;
            }
        });

        super.onCreateOptionsMenu(menu, inflater);
    }

Upvotes: 6

Pankaj Arora
Pankaj Arora

Reputation: 10274

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchItem.expandActionView();

and in menu file, use

 showAsAction="ifRoom|collapseActionView"

If any query, feel free to comment.

Upvotes: 18

Wakim
Wakim

Reputation: 1706

You can use the property android:iconifiedByDefault="false" on XML or set programatically setIconifiedByDefault(false). Acording to the documentation this property set the SearchView expanded like you want.

Take a look at SearchView.setIconifiedByDefault(boolean iconified)

Upvotes: 109

Related Questions