Android textview custom font – system built-in fonts

Android system itself built some fonts, android studio textstyle font, you can use in the program, and support the configuration custom font android xml when the font to modify the style. Android textview custom font, Italic three kinds of style, built-in normal, sans, serif, monospace, several fonts (measured in English only a few of these fonts effective), android: fontface, android: fontFamily, Typatic and fontFamily function the same.

How to Use a xml custom font in Android studio

The above way can change the font style, is not really custom. Android system supports TypeFace, that is, ttf font file. We can put in the program ttf font file, typeface in the program to set the font. 

The first step to set textview custom font, in the assets directory new fonts directory, the ttf font file into this.

How to set textview custom font using Android studio - How to set textview custom font using Android studio - How to set textview custom font using Android studio - android apps studio textview custom fonts

……

public class MainActivity extends AppCompatActivity {

     private TextView textView;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         textView= (TextView) findViewById(R.id.text);

         AssetManager assets = getAssets();

         Typeface fromAsset = Typeface.createFromAsset(assets, "fonts/name.ttf");

         textView.setTypeface(fromAsset);

    }

}

…..

For the need to use more places, you can write a subclass of TextView to unified treatment.

…..
public class CustomTextView extends TextView {

 
     public CustomTextView(Context context) {

          super(context);

          // TODO Auto-generated constructor stub

     }

 
     public CustomTextView(Context context, AttributeSet attrs) {

          super(context,attrs);

          // TODO Auto-generated constructor stub

     }
….

     

public CustomTextView(Context context, AttributeSet attrs,int defStyle) {

          super(context,attrs,defStyle);

          // TODO Auto-generated constructor stub

     }

 
     public void setTypeface(Typeface tf, int style) {

          super.setTypeface(AppContext.getInstance().getTypeface());

     }

 
}

 

// access the font

typeface = Typeface.createFromAsset(getAssets(), "fonts/fzlt.ttf");

Method or a bit shortcomings: can only replace a control font, if you need to replace the Button or EditText control font, you need to customize these controls in the same way, so heavy workload, how to efficiently replace the font in the app, see The following references.

Use ttf fonts in webview

  • For local pages, put font files in the asset directory, and add the following in css, customize a font face, and in the need to use the font face can be.
<style>

@font-face {

     font-family: 'myface';

     src: url('file:///android_asset/fonts/fzlt.ttf');

}

body {

     margin: 0;

     padding: 0;

     font-family:'myface','Founder Lanting fiber black simplified';

}

.textbar{ box-sizing:border-box; width:100%; padding:5px;}

.textbar p{ font-size:16px; text-align:justify; color:#333;

line-height:24px; margin:0 0 0 0;}

.textbar h1{ font-size:18px; margin:10px 0 10px 0;color:#000}

</style>

 

  • For online pages, you need to put the font file on the server, using the same definition font face, applied to each place.

In order to reduce the page or the server-side work, you can use the local injection into the font-face of the css, and the entire page to replace the style. To webview custom webViewClient, rewrite onPageFinish, in which to add the following:

view.loadUrl("javascript:!function(){" + "s=document.createElement('style');s.innerHTML="+ "\"@font-face{font-family:myhyqh;src:url('**injection**/hyqh.ttf');}*{font-family:myhyqh !important;}\";"

+ "document.getElementsByTagName('head')[0].appendChild(s);" +

"document.getElementsByTagName('body')[0].style.fontFamily = \"myhyqh\";}()");

 
// Since there is no permission on the page to access the local asset folder, so we need to intercept the request to load the local file, here I replace the `file:

// android_assets / `for` ** injection ** / `, we also need to override` shouldInterceptRequest`

/ / In the request for our font file, load the local file:

@Override

public WebResourceResponse shouldInterceptRequest (WebView view, String url){

     WebResourceResponse response = super.shouldInterceptRequest(view, url);

     Log.i("load intercept request:" + url);

     if (url != null && url.contains("**injection**/")) {

         //String assertPath = url.replace("**injection**/", "");

         String assertPath = url.substring(url.indexOf("**injection**/") + "**injection**/".length(), url.length());

         try {

               response = new WebResourceResponse("application/x-font-ttf", "UTF8", getAssets().open(assertPath));

         } catch (IOException e) {

               e.printStackTrace();

         }

     }

     return response;

}

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!