Branda
Branda

Reputation: 73

SearchView: go to another layout as enter is pressed in Android

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;
}

Upvotes: 0

Views: 94

Answers (1)

Jassem Ben Hamida
Jassem Ben Hamida

Reputation: 524

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

Upvotes: 1

Related Questions