First, I declare a lateinit var to hold a reference to ProgressDialog
private lateinit var progressDialog:ProgressDialog
And I instantiate in onCreate
override fun onCreate(savedInstanceState: Bundle?) {
progressDialog = ProgressDialog(requireContext())
progressDialog.setTitle("Downloading photos")
progressDialog.setMessage("It might take a few seconds..")
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
I used clearFocus to hide the keyboard, I also make the dialog visible. Then I also listen to focus change to collapse the searchview
searchView.apply {
setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(queryText: String): Boolean {
Log.d(TAG, "QueryTextSubmit: $queryText")
progressDialog.show()
photoGalleryViewModel.fetchPhotos(queryText)
clearFocus()
return true
}
override fun onQueryTextChange(queryText: String): Boolean {
Log.d(TAG, "QueryTextChange: $queryText")
return false
}
})
setOnFocusChangeListener { v, hasFocus ->
if (!hasFocus) {
searchItem.collapseActionView()
}
Finally, onViewCreated, we dimiss the dialog after downloading the json
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
photoGalleryViewModel.galleryItemLiveData.observe(viewLifecycleOwner,
Observer {galleryItems ->
Log.d(TAG, "Have gallery items from ViewModel $galleryItems")
progressDialog.dismiss()
photoRecyclerView?.adapter = PhotoAdapter(galleryItems)
})
}