Search code examples
androidsearchview

onQueryTextChange is not being called when I start typing in the search bar


I am pulling my hair out over this. I am trying to search my database with the search view widget. I have followed multiple examples but nothing is working for me. I am logging the onQueryTextChange function to see if it gets called after typing in the search bar but no luck. Can anyone see an issue here. Using room and koin if that helps.

My main Activity:

    import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupActionBarWithNavController
import com.durbindevs.tradiedairy.R
import com.durbindevs.tradiedairy.databinding.ActivityMainBinding
import com.durbindevs.tradiedairy.ui.roster.JobMotor
import org.koin.androidx.viewmodel.ext.android.viewModel

class MainActivity : AppCompatActivity(), SearchView.OnQueryTextListener {

    private val motor: JobMotor by viewModel()

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)

        supportFragmentManager.findFragmentById(R.id.nav_host)?.findNavController()?.let { nav ->
            appBarConfiguration = AppBarConfiguration(nav.graph)
            setupActionBarWithNavController(nav, appBarConfiguration)
        }

    }

    override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
        R.id.about -> {
            startActivity(Intent(this, AboutActivity::class.java))
            true
        }
        else -> super.onOptionsItemSelected(item)
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.action_search, menu)
        Log.d("search", "inflate")

            val search = menu?.findItem(R.id.search)
            val searchView = search?.actionView as? SearchView
            searchView?.isSubmitButtonEnabled = true
        searchView?.setIconifiedByDefault(false)
            searchView?.setOnQueryTextListener(this)

        return true


    }

    override fun onQueryTextSubmit(query: String?): Boolean {
       if (query != null){
            Log.d("search", "searching")
            searchDatabase(query)
       }
        return true
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        if (newText!= null) {
            Log.d("search", "searching")
            searchDatabase(newText)
        }
        return true

    }
    private fun searchDatabase(query: String) {
        val searchQuery = "%$query%"
        Log.d("search", "searching")
        motor.searchForItems(searchQuery)
    }

    override fun onSupportNavigateUp() =
        NavigationUI.navigateUp(findNavController(R.id.nav_host), appBarConfiguration)

}

my Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.durbindevs.tradiedairy">

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <application
        android:name=".JobApp"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.JobDairy">
        <activity android:name=".ui.AboutActivity" />
        <activity android:name=".ui.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
    </application>

</manifest>

My res menu file


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context =".ui.MainActivity">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/menu_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="ifRoom|withText"
        tools:targetApi="o"/>
</menu>


Solution

  • Just change the "actionViewClass" for the searchview item in your menu resource file to "androidx.appcompat.widget.SearchView" (the androidx namespace).

    Like this:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context =".ui.MainActivity">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/menu_search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom|withText"
        tools:targetApi="o"/>