Close the searchview if tab position get changed or by calling onPageChangeListener

Viewed 4813

I have one activity from where i am loading 6 fragments. Six of three fragments have the searchview and rest of the three have not implemented the searchview.

I have made two menu.xml in which

  • one having the searchview and commonitem for action bar and other
  • one does not have the searchview and contains common items

Now if i expand the searchview in the first tab and then move to the second tab,searchview still get opened(i know it is the searchview of the second tab as i have implemented the onCreateOptionMenu() for the each fragments),which i don't want.

What i want is when i move to the first fragment to second or third at that time searchview should get collapsed and only click of the searchview(in current fragment it should get expanded).

Moreover, if i move to the first fragment to 2nd,3rd and 4th(my account info fragment,not contain searchview in action bar),then in 4th fragment i still get the searchview in open mode...

Another thing that i want to know,by clicking on the searchview it gets expanded and also popup the soft keyboard,so is this is the default behavior of the searchview (expanding and open keyboard)?

I want to open keyboard when i click on the text(edit text) area of the searchview.

Hope m clear.... Any Suggestion/Links will be appreciated...

Let me know if any more detail you required from my side.....

EDIT ::

Applying S.D.'s solution

Testing on 2.3.6

1)When i am on the first fragment(First Tab)

enter image description here

2)After Expanding it in same fragment ::

enter image description here

3)Move to the next Fragment(Tab,by keeping the searchview opened) ::

enter image description here

Testing on the 4.2.1 ::

1)When i am on the first fragment(First Tab)

enter image description here

2)After Expanding it in same fragment ::

enter image description here

3)Move to the next Fragment(Tab,by keeping the searchview opened) ::

enter image description here

i have set 'setIconified(true)' in my Container Activity(from where my all fragment loded) in below methods ::

@Override
public void onPageSelected(int position)
{
    if (searchView!=null && !searchView.isIconified()) {  //true == searchView closed
        searchView.setIconified(true);  
        searchView.setIconified(true);  
    }
    actionBar.setSelectedNavigationItem(position);
}

@Override
public void onTabSelected(Tab tabposition, FragmentTransaction fragmentposition) {
    if (searchView!=null && !searchView.isIconified()) {  //true == searchView closed
        searchView.setIconified(true);  
        //searchView.setIconified(true);  
    }
    awesomePager.setCurrentItem(tabposition.getPosition());
}
6 Answers

you have to call menues seperately..

This is a menu code

   <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".HomeActivity"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_search_black_24dp"
            app:showAsAction="ifRoom|collapseActionView"
            app:actionViewClass="androidx.appcompat.widget.SearchView"
            android:title="Search"/>
    </menu>

Change the code to Fragment 1 and Fragment 2

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.action_search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager =
                (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        androidx.appcompat.widget.SearchView searchView =
                (androidx.appcompat.widget.SearchView) menu.findItem(R.id.action_search).getActionView();

        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

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

                @Override
                public boolean onQueryTextChange(String newText) {
                    //your search code
                    return true;
                }
            });
        }

        super.onCreateOptionsMenu(menu, inflater);
    }

Have a nice day!!

Related