How to Build a CRUD App With Kotlin and Android Studio

Chapter 17 Searching

Problems covered in this chapter
  • Searching database values

  • Show/hide menu items

In Chapter 7 we defined the toolbar menu (file menu_toolbar.xml). In the last chapter we activated the Help and Preferences menu items when the user is at the home screen of our app. In the toolbar menu we also defined a search view which we will activate in this chapter so the user can search items in the corresponding lists. You will be surprised - it takes only a couple of additional lines and we are done! We simply extend MainActivity and we can search persons and tags:

17.1 MainActivity.kt

Listing 118: MainActivity.kt
...
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_toolbar, menu)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val fragment = navHostFragment.childFragmentManager.fragments[0]
        val prefMenuItem = menu!!.findItem(R.id.updatePreferencesFragment)
        val helpMenuItem = menu.findItem(R.id.helpFragment)
        if ( fragment is HomeFragment) {
            Log.d(TAG,"We are in HomeFragment")
            prefMenuItem.isVisible = true
            helpMenuItem.isVisible = true
        } else {
            Log.d(TAG,"We are not in HomeFragment")
            prefMenuItem.isVisible = false
            helpMenuItem.isVisible = false
        }
        when (fragment) {
            is PersonsListFragment -> searchMenuItem.isVisible = true
            is TagsListFragment -> searchMenuItem.isVisible = true
            else -> searchMenuItem.isVisible = false
        }
        searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(p0: String?): Boolean {
                GlobalAppStore.someFilterableAdapter!!.filter.filter(p0)
                searchView.clearFocus()
                return false
            }
            override fun onQueryTextChange(p0: String?): Boolean {
                GlobalAppStore.someFilterableAdapter!!.filter.filter(p0)
                return false
            }
        })
        return super.onCreateOptionsMenu(menu)
    }
...

When you install the app to a (virtual) device, you will spot a magnifying glass on top of the persons and tags lists (see Figure 17.1).

Tags list with magnifying glass
Figure 17.1: Tags list with magnifying glass

When you tap on the magnifying glass and enter a search term, the list is narrowed to items matching your term (see Figure 17.2).

Entering search term narrows list
Figure 17.2: Entering search term narrows list

If you enter a search term that none of your items matches, you get a corresponding message (see Figure 17.3).

No match message
Figure 17.3: No match message

When you search tags, your search term is matched with tag titles. However, if you search persons, your search term matches the person’s name as well as the person’s birthdate. Give it a try!