6

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());
}
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62

6 Answers6

5

For closing your SearchView you can use searchview.setIconified(true). So in your fragments onPause you can call:

 public void onPause() {  //not sure if you should use onDestroyView() instead
    super.onPause();

    if (!yourSearchView.isIconified()) {  //true == searchView closed
        yourSearchView.setIconified(true);  
    }
 }

Custom behavior of the SearchView is to show your keyboard. If you click on the Search Icon, the SearchView will automatically popup and the Search TextField will be focused which opens the Keyboard automatically. However you should be able to remove the focus of the textview which will close the keyboard. check here

Community
  • 1
  • 1
longi
  • 11,104
  • 10
  • 55
  • 89
  • @longillong i have set your solution in `onPause()` and `onDestroyView()`.But found the same result.... any idea why such behaviour is happening? – AndroidLearner Dec 30 '13 at 06:03
5

Search view implementation is a bit strange I'm not sure it is a bug or intended feature. To collapse SearchView properly, you have to call setIconified(true) twice. First call will sometime clear the text, the second will then actually collapse it.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • where should i call it? in `onPause()` or somewhere else? – AndroidLearner Jan 01 '14 at 09:09
  • @AndroidLearner I think you should call that where you are changing fragments for tabs. – S.D. Jan 01 '14 at 09:19
  • Please check my edit,i have tried your suggestion and which gives me something strange output.any idea why such kind of behavior happening? – AndroidLearner Jan 01 '14 at 10:01
  • @AndroidLearner ViewPager may keep more than one fragments around at a time, this might result in 2 search views added to action menu. Keeping a separate search view for all fragments is not a good idea. You can keep a search view in parent activity's menu grant its reference to attached fragments. – S.D. Jan 03 '14 at 15:32
3
MenuItem menuSearch = menu.findItem(R.id.itemSearch);
SearchView searchView = (SearchView) menuSearch.getActionView();
menuSearch.collapseActionView();

Have you tried collapseActionView() method the above way? You need to call this method where you're changing your tabs. Let me know if you're still having issues.

Avi Singh
  • 111
  • 6
  • thanks for your reply. I have tried this way earlier also and found the result which i have pasted above in my edit(please refer the image 3 testing on 2.3.6,where searchicon on left side) and also the soft keyboard shown... – AndroidLearner Jan 03 '14 at 06:07
  • @AndroidLearner Have you tried using searchView.onActionViewCollapsed() ? – Avi Singh Jan 03 '14 at 06:10
2
private void setTabListener() {

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                invalidateOptionsMenu();

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
1

When the SearchView is used in an ActionBar as an action view for a collapsible menu item, it needs to be set to iconified by default using setIconifiedByDefault(true). This is the default, so nothing needs to be done.

Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.

Source

http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)

venkat530
  • 43
  • 2
  • 10
0

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!!