0

I'm trying to launch a query to retrieve data from an online database, but I can't understand how, with the SearchView, go to another layout as a key (in this case enter) is pressed. I thought that maybe it has, like the button, a clickListener that when clicked (after setting the mobile_navigation.xml) you can set in which layout go, but there's not. Here's my code:

Context myContext = this;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Insert here the title");

searchView.setOnQueryTextListener(new OnQueryTextListener() {

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        query = query.replace(' ', '+');
        Log.d("Query", query);
        //Navigation.createNavigateOnClickListener(R.id.action_nav_home_to_searchFragment, null);
        ComponentName cm = new ComponentName(myContext, SearchFragment.class);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(cm));
        return false;
    }

});

return true;
}
Kasım Özdemir
  • 4,028
  • 3
  • 12
  • 24
Branda
  • 63
  • 5
  • You want to open a new layout to perform search? – Rahul Agrawal May 21 '20 at 08:53
  • I don´t understand the approach... do you want to navigate to another screen que the user select a key using SearchView? Maybe using the ascii code, I think the enter key is the 10 value in char. – Manuel Mato May 21 '20 at 09:01
  • Check this answer: https://stackoverflow.com/a/43141720/7877039 You can just check in the if statement for the correct key and transition from there. – Johan Ferreira May 21 '20 at 09:32
  • @Rahul Agrawal no, I want to do the search in a layout A, and open a new layout B when the enter button is pressed (so when I'm on onQueryTextSubmit). – Branda May 21 '20 at 09:58

1 Answers1

1

You can't use createNavigateOnClickListener on theonQueryTextSubmit because the enter button is already clicked. But you can use it using setOnSearchClickListener instead of setOnQueryTextListener

searchView.setOnSearchClickListener(Navigation.createNavigateOnClickListener(R.id.btnLogin, null))