Android - Can't find TextView inside SearchWidget when using Android support library

I used the following snippet to find the TextView inside the SearchView widget.

    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null);
    mQueryTextView = (AutoCompleteTextView) searchView.findViewById(autoCompleteTextViewID);

      

However, when switching to the support library, android.support.v7.appcompat

it no longer works.

I think this is because the support library does not use a prefix android:

for "android: id / search_src_text", but I have no idea what it should be. I tried

getResources().getIdentifier("android.support.v7.appcompat:id/search_src_text", null, null);

      

PS Additional code snippets:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // this xml has funshion:actionViewClass="android.support.v7.widget.SearchView"
        getMenuInflater().inflate(R.menu.search_activity_actions_v7, menu);

        android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) 
                MenuItemCompat.getActionView(menu.findItem(R.id.action_search_v7));
        if (searchView==null){
            FSLogcat.e(TAG, "searchView is null!");
        }else{
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
            //searchView.requestFocus();
            searchView.setSubmitButtonEnabled(true);

            findSearchViewTextView(searchView);
            updateSearchViewText();
        }   

    return super.onCreateOptionsMenu(menu);
}

private void findSearchViewTextView(View searchView) {
    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null);
    mQueryTextView = (AutoCompleteTextView) searchView.findViewById(autoCompleteTextViewID);
}

private void updateSearchViewText() {       
    if (mQueryTextView == null){
    } else {    
        mQueryTextView.setText(mQuery);
    }

}

      

Note that the SearchView widget makes it difficult to find suggestions inside the TextView

//      in SearchAutoComplete
//        /**
//         * We override this method to avoid replacing the query box text when a
//         * suggestion is clicked.
//         */
//        @Override
//        protected void replaceText(CharSequence text) {
//        }

      

UPDATE: The need to handle SearchWidget came after SearchResultsActivity got SearchWidget as SearchResultsActivity. While perhaps this should be implemented as one activity in the next iteration, for the current version this week I just need to address the usage issue i.e. Make sure the TextView inside SearchWidget on SearchResultsActivity always has the most recent request.
That is, this is critical code, if it breaks it will be overwritten, but definitely not by cloning the default widget. It should be in a different way.

+3


source to share


1 answer


As Pedro Oliveira said

mQueryTextView = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);

      



the one-liner worked.

+8


source







All Articles