Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
}
like image 907
AndroidLearner Avatar asked Dec 25 '13 09:12

AndroidLearner


2 Answers

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.

like image 115
S.D. Avatar answered Sep 22 '22 09:09

S.D.


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

like image 32
longi Avatar answered Sep 19 '22 09:09

longi