learner
learner

Reputation: 11780

How to style android.support.v7.widget.SearchView

I want to set the tint of the searchIcon, the color of the cursor, the layout of the suggestionRow (i.e. size, color, icon). How do I take control of the look of my SearchView and its suggestion drop down?

So far I have:

SearchView

 <android.support.v7.widget.SearchView
    
    android:id="@+id/search_view"

        android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    
    android:background="@color/primary_light"
    
    >

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.example.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>

update

So I found the following that helped a little, but I still cannot set the size of the text in the dropdown

SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

Upvotes: 2

Views: 2449

Answers (2)

Joan Casadell&#224;
Joan Casadell&#224;

Reputation: 286

I have a method in my Utils that do some style changes to SearchView. Maybe this can help you.

I advise you that this method dosen't care with suggestions. Only for some points of style SearchView:

public static SearchView customizeWhiteSearchView(SearchView sv, Context context, String hintText){

    EditText searchEditText = (EditText) sv.findViewById(android.support.v7.appcompat.R.id.search_src_text);  
    searchEditText.setTextColor(Color.WHITE);  
    searchEditText.setHintTextColor(context.getResources().getColor(R.color.light_light_gray));  

    ImageView imgViewSearchView = (ImageView) sv.findViewById(android.support.v7.appcompat.R.id.search_button);
    imgViewSearchView.setImageDrawable(context.getResources().getDrawable(R.drawable.button_cerca));

    SpannableStringBuilder ssb = new SpannableStringBuilder("   ");
    if (hintText != null){ ssb.append(hintText); }
    else{ ssb.append("Mínim 3 caràcters..."); }

    Drawable searchIcon = context.getResources().getDrawable(R.drawable.button_cerca);
    int textSize = (int) (searchEditText.getTextSize() * 1.25);
    searchIcon.setBounds(0, 0, textSize, textSize);
    ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    searchEditText.setHint(ssb);

    ImageView close = (ImageView) sv.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    close.setImageResource(R.drawable.button_close);

    return sv;
}

Upvotes: 2

Avazxon Ubaydullayev
Avazxon Ubaydullayev

Reputation: 89

Try this:

 <item
        android:id="@+id/action_search"
        android:orderInCategory="200"
        android:title="Search"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>

Upvotes: -1

Related Questions