Styling a SearchView in Android Action Bar

后端 未结 11 1573
小鲜肉
小鲜肉 2020-12-02 20:56

I have a search widget in my Action Bar like this:

\"Search

(1) How do I change

相关标签:
11条回答
  • 2020-12-02 20:57

    I have been spending many time for this but finally: :-)

    1. To change the text color :
    ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
    

    or this one for AndroidX:

    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
    
    1. To change the text hint:
    ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
    

    or this one for AndroidX:

    ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
    
    0 讨论(0)
  • 2020-12-02 20:58

    You can change the style of the searchview. Here is some code I used to tweak the edittext, the voice icon...

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
            SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    
            int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
            searchView.findViewById(searchPlateId).setBackgroundResource(R.drawable.textfield_search_selected);
    
            int voiceSearchPlateId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
            searchView.findViewById(voiceSearchPlateId).setBackgroundResource(R.drawable.textfield_search_right_selected);
    
            // change hint color
            int searchTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
            TextView searchTextView = (TextView) searchView.findViewById(searchTextViewId);
            searchTextView.setHintTextColor(getResources().getColor(R.color.light_grey));
    

    [edit] A more complete answer is available here : Changing the background drawable of the searchview widget

    0 讨论(0)
  • 2020-12-02 21:03

    The way I solved this was by using Holo.Light.DarkActionBar theme. I don't believe you can easily change the search widget otherwise.

    0 讨论(0)
  • 2020-12-02 21:03

    in onCreateOptionsMenu :

    int id = searchView.getContext()
                       .getResources()
                       .getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);
    
    0 讨论(0)
  • 2020-12-02 21:07
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    searchView.setSubmitButtonEnabled(true); // to enable submit button
    
    ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
    b.setImageResource(android.R.drawable.ic_menu_search); //to change submit button icon
    
    TextView a=(TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    a.setTextColor(...); //to change color of search text
    
    0 讨论(0)
  • 2020-12-02 21:08

    If you're using appcompat library, then the solution is a bit different from Jerome's answer. Here's my solution

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        restoreActionBar();
    
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    
        SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchAutoComplete.setHintTextColor(Color.WHITE);
        searchAutoComplete.setTextColor(Color.WHITE);
    
        View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
        searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);
    
        ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        searchCloseIcon.setImageResource(R.drawable.clear_search);
    
        ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
        voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);
    
        ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
        searchIcon.setImageResource(R.drawable.abc_ic_search);
    
    
        return super.onCreateOptionsMenu(menu);
    }
    
    0 讨论(0)
提交回复
热议问题