21

I have been trying to set custom font to the android.support.v7.widget.SearchView query hint and the text entered in the View.I did try setting the font dynamically from the assests to the searchView by creating a TypeFace object, but the problem occurs that "SearchView doesn't contain a setTypeface(Typeface tf) method." I did try for a custom SearchView class but couldn't find one.

 private void setFont(String font1, String font2, String font3)
 {
        Typeface tf = null;

        tf = UiUtil.changeFont(MainActivity.this, font1);
        btn1.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font2);
        btn2.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font3);
        btn3.setTypeface(tf);

         tf = UiUtil.changeFont(MainActivity.this, font3);
        // no set typeface method..

    }
Apurva
  • 7,871
  • 7
  • 40
  • 59
AndroidMech
  • 1,676
  • 3
  • 20
  • 31

5 Answers5

55

The workaround here is to first get the searchView's textView and then change the typeface for the textView instead:

 TextView searchText = (TextView)
 searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 Typeface myCustomFont = Typeface.createFromAsset(getAssets(),"fonts/myFont.ttf");
 searchText.setTypeface(myCustomFont);

Or, if you're not using appcompatv7:

 int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
 TextView searchText = (TextView) searchView.findViewById(id);

then set the typeface as usual.

irvanjitsingh
  • 1,082
  • 1
  • 12
  • 19
  • 4
    While I assume this works, you're assuming implementation details of the SearchView class. These could change at any time, it isn't a safe assumption. – Gabe Sechan Jun 08 '17 at 15:42
  • 6
    For androidX, the ID to search for is androidx.appcompat.R.id.search_src_text – amitavk Mar 14 '19 at 10:32
  • 3
    @amitav13 I get a null `TextView` in AndroidX if I use `androidx.appcompat.R.id.search_src_text` as ID. However, the second method that uses `getIdentifier()` works for me exactly as it is suggested. I don't have to change `android:id/search_src_text` to `androidx.appcompat.R.id.search_src_text`. Any ideas why `androidx.appcompat.R.id.search_src_text` ain't working? – Shahood ul Hassan Mar 22 '19 at 10:49
5

To change the font family in searchview programmatically from xml font folder:

Typeface tf = ResourcesCompat.getFont(dis,R.font.montserrat_regular);
TextView searchText = (TextView)search_view.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTypeface(tf);
ItsPete
  • 2,363
  • 3
  • 27
  • 35
murugan mani
  • 375
  • 5
  • 6
4

For Kotlin and Androidx

val face: Typeface = Typeface.createFromAsset(context?.assets, "font.otf")
val searchText = searchView.findViewById<View>(androidx.appcompat.R.id.search_src_text) as TextView
searchText.typeface = face
Jose Q
  • 441
  • 1
  • 4
  • 14
1

Thanks to all the previous answers, I found my best solution witch I hope that you find it the cleanest answer, too.

This is working for SearchView class inside the package android.widget

First, create an extension like below:

import android.graphics.Typeface
import android.widget.SearchView
import android.widget.TextView

fun SearchView.setTypeFace(typeface: Typeface?) {
    val id = context.resources.getIdentifier("android:id/search_src_text", null, null)
    val searchText = searchView.findViewById(id) as TextView
    searchText.typeface = typeface
}

Then use this extension wherever you like:

val typeface = ResourcesCompat.getFont(requireContext(), R.font.sans)
searchView.setTypeFace(typeface)

You may want to get typeface from assets or other ways. but the rest is the same.

Emad Razavi
  • 1,903
  • 2
  • 17
  • 24
0

If anyone looking to implement the same in xml

Try the solution mentioned this answer able to customise {textSize, fontFamily, textColor, hideBottomLine} in SearchView - https://stackoverflow.com/a/73997430/3020859

enter image description here

ramji
  • 1,982
  • 2
  • 13
  • 13