How to use custom font in String.xml file

I have the following code in a String.xml file. Now my question is here, I had a font face like Aerial.So is there a way that I can use my own font instead of using an antenna here. this is aa.ttf ... since I can use it here. My code

<string name="strt_dialog_desc">To start monitoring press and hold the date on which the latest cycle started.<![CDATA[<font face=Aerial>]]>Click OptionMenu for help!<![CDATA[</font>]]></string> 
+1
source share
4 answers

// put the font in the resource folder

 TextView tv=(TextView)findViewById(R.id.custom); Typeface face=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf"); tv.setTypeface(face); tv.setText(R.string.strt_dialog_desc); 
+3
source

// dynamically use SpannableString to change the font

 Typeface tfaerial=Typeface.createFromAsset(getAssets(),"fonts/aerial.ttf"); Typeface tfTradeGothicLight=Typeface.createFromAsset(getAssets(), "fonts/TradeGothic-Light.OTF"); String strt_dialog_desc=this.getResources().getString(R.string.strt_dialog_desc); int upto=strt_dialog_desc.indexOf("."); if (strt_dialog_desc!=null) { aboutAuthTV.setTextColor(Color.BLACK); aboutAuthTV.setLineSpacing(1.2f, 1.5f); aboutAuthTV.setTextSize(23); SpannableString SS = new SpannableString(strt_dialog_desc); SS. setSpan ( new StyleSpan(tfTradeGothicLight.getStyle()), 0, upto,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); SS. setSpan ( new StyleSpan(tfaerial.getStyle()), upto, strt_dialog_desc.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE); yourtextView.setText(SS); } 

// this is for changing the color so the font change does not work

 String str="<font size =\"20\"><B>Bold</B> <br/> Then Normal Text<br/> <i>Then Italic</i> </font>" + "<br/> <font color=\"green\" >this is simple sentence </font>" + "<br/> <font face=\"verdana\" >this is simple sentence </font>"+ "<br/><br/><br/><br/><a>this is simple sentence</a>"; Spanned strHtml= Html.fromHtml(str); TextView tv = (TextView)findViewById(R.id.textView); tv.setText(strHtml); 
+2
source

I think you can do the same using this code , this will change the font at runtime.

Myapp.java

 public class MyApp extends Application { @Override public void onCreate() { TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf } } 

TypefaceUtil.java

 import android.content.Context; import android.graphics.Typeface; import android.util.Log; import java.lang.reflect.Field; public class TypefaceUtil { /** * Using reflection to override default typeface * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN * @param context to work with assets * @param defaultFontNameToOverride for example "monospace" * @param customFontFileNameInAssets file name of the font from assets */ public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) { try { final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets); final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride); defaultFontTypefaceField.setAccessible(true); defaultFontTypefaceField.set(null, customFontTypeface); } catch (Exception e) { Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); } } } 

themes.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light"> <!-- you should set typeface which you want to override with TypefaceUtil --> <item name="android:typeface">serif</item> </style> </resources> 
+2
source

You can set the font with the following attribute

 android:typeFace= 
-1
source

Source: https://habr.com/ru/post/1443644/


All Articles