user11219955
user11219955

Reputation:

Android -java OnQueryTextSubmit() not called

I searched for all the stack overflow answers regarding my issue but no luck. I want to just use search view and search data. Menu is inflated correctly. But onQueryTextSubmit() is not called when i submit my search query. i donno where i'm going wrong. Also i don't see submit button on searchview.

This is the way

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);

        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView =(SearchView) item.getActionView();
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        Toast.makeText(SearchDonorsActivity.this, query, Toast.LENGTH_SHORT).show();
        searchUsers(query);
        return false;
    }

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

This is the app_bar layout

<?xml version="1.0" encoding="utf-8"?>
    
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="Search Donors"
            app:menu="@menu/menu_main"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            />
    
    </com.google.android.material.appbar.AppBarLayout>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

help me around with this

Upvotes: 2

Views: 713

Answers (1)

Adhiambo Oyier
Adhiambo Oyier

Reputation: 235

Well, since you are using menu item, I feel that your implementation is not complete. You need to manage it as a search widget, which is not obvious from your code. My implementation of this is as follows:

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)

    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    (menu.findItem(R.id.action_search)?.actionView as SearchView).apply {
        queryHint = getString(R.string.search_donor_name)
        setSearchableInfo(searchManager.getSearchableInfo(componentName))
        setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (findNavController(R.id.nav_host_fragment).currentDestination?.id == R.id.fragmentName) {
                    Toast.makeText(SearchDonorsActivity.this, query, Toast.LENGTH_SHORT).show()
                    searchUsers(query)
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (findNavController(R.id.nav_host_fragment).currentDestination?.id == R.id.fragmentName) {
                    Toast.makeText(SearchDonorsActivity.this, query, Toast.LENGTH_SHORT).show()
                    searchUsers(query)
                }
                return true
            }
        })
    }
    // Hide search and show only in fragments with searchable lists
    menu.findItem(R.id.searchItem)?.isVisible = false

    return super.onCreateOptionsMenu(menu)
}

This snippet is for a single activity app with multiple fragments, which is why I am checking for the current destination before I get the query.

Upvotes: 1

Related Questions