Chapter 27 Search
Polishing Your App
For a little bit of polish, pre-populate the search text box with the saved query when the user presses on
the search icon to expand the search view. SearchView’s View.OnClickListener.onClick() method
is called when the user presses the search icon. Hook into this callback and set the SearchView’s query
text when the view is expanded.
Listing 27.15 Pre-populating SearchView (PhotoGalleryFragment.java)
public class PhotoGalleryFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
});
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String query = QueryPreferences.getStoredQuery(getActivity());
searchView.setQuery(query, false);
}
});
}
}
Run your app and play around with submitting a few searches. Revel at the polish your last bit of code
added. Of course, there is always more polish you could add....
Challenge: Polishing Your App Some More
You may notice that when you submit a query there is a bit of a lag before the RecyclerView starts to
refresh. For this challenge, make the response to the user’s query submission feel more immediate. As
soon as a query is submitted, hide the soft keyboard and collapse the SearchView.
As an extra challenge, clear the contents of the RecyclerView and display a loading indicator
(indeterminate progress bar) as soon as a query is submitted. Get rid of the loading indicator once the
JSON data has been fully downloaded. In other words, the loading indicator should not show once your
code moves on to downloading individual images.