Example usage for android.widget SearchView getResources

List of usage examples for android.widget SearchView getResources

Introduction

In this page you can find the example usage for android.widget SearchView getResources.

Prototype

public Resources getResources() 

Source Link

Document

Returns the resources associated with this view.

Usage

From source file:com.numenta.taurus.instance.InstanceListActivity.java

private void configureSearchView(@NonNull final SearchView searchView) {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    // Handle query events
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override// www.  j a va  2 s . c  o m
        public boolean onQueryTextSubmit(String query) {
            // Hide Keyboard on submit
            InputMethodManager imm = (InputMethodManager) searchView.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            }

            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // Filter list as the user types
            _listFragment.applyFilter(newText);
            return true;
        }
    });

    // FIXME: Android does not support styling the search view across all versions.
    // For now, "peek" into internal API to make the appropriate changes to the SearchView.
    // In the future we should use the official android API to customize the SearchView widget.
    // See android.R.layout.search_view for the layout we are "peeking". It is no guarantee it
    // will work on all public android versions and/or OEM customizations.
    // This HACK is only valid for the POC phase. We should find a better solution before releasing
    Resources resources = searchView.getResources();

    // Style search box and text
    int searchPlateId = resources.getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        int searchTextId = resources.getIdentifier("android:id/search_src_text", null, null);
        TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        if (searchText != null) {
            searchPlate.setBackgroundResource(android.R.drawable.editbox_background);
            searchText.setPadding(5, 0, 0, 0);
            searchText.setTextColor(Color.BLACK);
            searchText.setHintTextColor(Color.LTGRAY);
        }
    }
}