Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView custom font in android

Tags:

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..      } 
like image 399
AndroidMech Avatar asked Apr 03 '15 06:04

AndroidMech


2 Answers

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.

like image 66
irvanjitsingh Avatar answered Sep 16 '22 13:09

irvanjitsingh


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); 
like image 20
murugan mani Avatar answered Sep 19 '22 13:09

murugan mani