Polishing Your App

For a little bit of polish, pre-populate the search text box with the saved query when the user presses the search icon to expand the search view.

First, add a computed property to PhotoGalleryViewModel to expose the search term the UI is currently displaying results for.

Listing 26.15  Exposing the search term from PhotoGalleryViewModel (PhotoGalleryViewModel.java)

class PhotoGalleryViewModel(private val app: Application) : AndroidViewModel(app) {
    ...
    private val mutableSearchTerm = MutableLiveData<String>()

    val searchTerm: String
        get() = mutableSearchTerm.value ?: ""

    init {
        ...
    }
    ...
}

SearchView’s View.OnClickListener.onClick() function is called when the user presses the search icon. Hook into this callback ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.