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….

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.252.37