Search code examples
androidfontssearchview

Android - SearchView - setTypeface not available


For some reason, I'm not able to setTypeface on a SearchView. Here is my code to try to change the font:

searchView = findViewById(R.id.searchview_invite_friends_search_contact);
Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
searchView.setTypeface(myFont);

I defined it like this in a the xml:

<SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:queryHint="@string/searchview_invite_friends_search_contact"
        android:iconifiedByDefault="false"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:searchIcon="@drawable/icon_search"
        android:focusable="false"
        android:layout_height="45dp"
        android:queryBackground="@color/transparent"
        android:gravity="center_horizontal"
        >

Solution

  • The SearchView contains a TextView which you first have to find to change its Typeface like this.

     TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
     searchText.setTypeface(<your Typeface>);
    

    This is for the SearchView in the v7 Android Support library.

    For the normal SearchView in the android.widget package see https://stackoverflow.com/a/30915643/3233251