Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 430
Paul Verest Avatar asked Sep 09 '14 10:09

Paul Verest


2 Answers

As Pedro Oliveira suggested

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

one-liner just worked.

like image 64
Paul Verest Avatar answered Oct 20 '22 09:10

Paul Verest


kotlin one line

mQueryTextView = findViewById<AutoCompleteTextView>(androidx.appcompat.R.id.search_src_text)
like image 20
bo0k Avatar answered Oct 20 '22 09:10

bo0k