Android - cannot find TextView inside SearchWidget when using Android Support library
Asked Answered
E

3

5

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.

Excusatory answered 9/9, 2014 at 10:57 Comment(5)
Try using AutoCompleteTextView mQueryTextView = (AutoCompleteTextView) mSearchView.findViewById(R.id.search_src_text); insteadTruncate
The right answer is for you to fork SearchView and add what you want to your own copy, rather than assuming that Google will never change their implementation in ways that break your code.Whimwham
Well putting things like that the best is to always fork support library and used it instead of Google's. ;)Truncate
Added UPDATE into questionsExcusatory
Have you tried to use R.id.search_src_text instead?Truncate
E
14

As Pedro Oliveira suggested

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

one-liner just worked.

Excusatory answered 10/9, 2014 at 2:29 Comment(2)
Not working for me. Still getting null: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.AutoCompleteTextView at org.example.appname.MainActivity.onCreateOptionsMenu(MainActivity.kt:78)Dacoity
@Dacoity it refers to the search layout inside searchview . try to define new style for searchview in style.xml. then add item name "layout" . you have to pass xml layout that you have created before for searchview. you have to use id like "search_src_text" for your view. I know it is so complex. it is layout file: justpaste.it/6c7fu . finally add that style to theme: <item name="searchViewStyle">@style/customsearchviewstyle</item> . another way might be work use : androidx.appcompat.R.id.search_src_textCockrell
S
6

kotlin one line

mQueryTextView = findViewById<AutoCompleteTextView>(androidx.appcompat.R.id.search_src_text)
Saprolite answered 7/4, 2021 at 23:9 Comment(0)
K
1

you can use Resource#getSystem to get the View

Resources.getSystem().getIdentifier("search_src_text",
    "id", "android")
Kissiah answered 2/1, 2020 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.