Search code examples
androidsearchandroid-support-librarysearchview

Android - cannot find TextView inside SearchWidget when using Android Support library


I used following snippet to find TextView inside SearchView widget.

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

However when switching to android.support.v7.appcompat support library, it does not work any more.

I guess it is because support library does not use android: prefix for "android:id/search_src_text", but I have no idea what should it be. I tried

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

P.S. More 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 SearchView widget makes it hard to get suggestions to be put inside 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 for SearchWidget manipulation arrived after SearchResultsActivity got the SearchWidget as SearchResultsActivity. While possibly the should be implemented as one Activity in the next iteration, for current release due in this week I just need to solve usage issue i.e. to make sure that TextView inside SearchWidget on SearchResultsActivity always has the latest query.
That is, it is critical code if it breaks, it will be rewritten, but definitely not by cloning standard widget. The should be other way.


Solution

  • As Pedro Oliveira suggested

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

    one-liner just worked.