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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.