0

I am trying to implement an Action bar search on a listview which is nested inside a Fragment. For now I am just trying to print the search text onto console, but its not working.

Following is my code:

main.xml:

<item
        android:id="@+id/search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@drawable/ic_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:title="search"/>

Class Def:

public class FragmentTab1 extends Fragment{ 

this is my onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
        System.out.println("menu inflater");
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.main, menu); 
        SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
        

        SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                System.out.println("Text"+newText);
                
                return true;
            }
            @Override
            public boolean onQueryTextSubmit(String query) {
                
                System.out.println("on query submit: "+query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(textChangeListener);
        return true;
    }

However when I type in the search box, nothing gets written onto the console, any hints what I am missing?

Community
  • 1
  • 1
User3
  • 2,465
  • 8
  • 41
  • 84

2 Answers2

0

Try This Code.

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    return true;
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • The problem here is with the method Declaration, in a fragment we have to use: `@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { ` With this everything is working as should be, however I have to look for a way that my MainActivity menu gets invalidated as soon as the Fragment is up. – User3 Oct 22 '14 at 11:41
  • u mean to say after writing some text in action bar search and then it will up to fragment from your activity. – Jay Rathod Oct 22 '14 at 11:55
  • No, I have a mainActivity to which three fragments are bound, I want search specific to lists which exist in these fragments and not a universal search - based on the parent activity. – User3 Oct 22 '14 at 11:59
  • what u have inflate in your fragment layout xml file? – Jay Rathod Oct 22 '14 at 12:05
0

The problem here is with the method Declaration, in a fragment we have to use:

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

With this everything is working as should be, however I have to look for a way that my MainActivity menu gets invalidated as soon as the Fragment is up.

User3
  • 2,465
  • 8
  • 41
  • 84