Using Custom Font in Android Textview Using Xml

How to set custom font in .xml file instead of .java file?

For your reference,

 public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyTextView(Context context) {
super(context);
init();
}

public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
setTypeface(tf ,1);

}
}

In XML,

 <you_package.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Your text"
/>

I need help in the custom Font and TextView

you can put "your_font.ttf" file in asset folder then import it with

Typeface custom_font_1 = Typeface.createFromAsset(getAssets(),  "your_font.ttf");

then assign it to your showCaseTextView with this

   showCaseTextView.setTypeFace(custom_font_1);

then in your onClickListener of showCaseTextView to change your specifiedTextView font do like this

   specifiedTextView.setTypeFace(custom_font_1);

and repeat it for other fonts.

The new Custom Font Method in android using xml

1.)Can this work in android lollipop or marshmallow

Ans : It's work for lower Version also(Based on my research).

2.)Is any support library needed for implementing the features

Ans : No need support Lib only you need to update your sdk(Android O)

3.)What type of font extension will this support like .ttf .otf

Ans: Yes, Its support .ttf, .otf font Files.

For more info you can see below links :

Android Doc for Font Family Api , Android-O Preview Video

How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black" // roboto black
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)

Sample Image

in combination with

android:textStyle="normal|bold|italic"

this 16 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Black
  • Roboto-Black italic
  • Roboto-Medium
  • Roboto-Medium italic

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="font_family_light">sans-serif-light</string>
<string name="font_family_medium">sans-serif-medium</string>
<string name="font_family_regular">sans-serif</string>
<string name="font_family_condensed">sans-serif-condensed</string>
<string name="font_family_black">sans-serif-black</string>
<string name="font_family_thin">sans-serif-thin</string>
</resources>

Using a custom typeface in Android

Is there a way to do this from the
XML?

No, sorry. You can only specify the built-in typefaces through XML.

Is there a way to do it from code in
one place, to say that the whole
application and all the components
should use the custom typeface instead
of the default one?

Not that I am aware of.

There are a variety of options for these nowadays:

  • Font resources and backports in the Android SDK, if you are using appcompat

  • Third-party libraries for those not using appcompat, though not all will support defining the font in layout resources

How to use custom font in android xml?

The best way i found by googling is- Say if you want to use in TextView then we have to extend the Textview and have to set the font in that later we can use our customised Textview in our xml. I'll show the extended TextView below

package com.vins.test;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyTextView(Context context) {
super(context);
init();
}

private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}

}

We calling init() to set font in each of the costructors.
Later we have to use this in our main.xml as shown below.

<com.vins.test.MyTextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>

Update:

Be aware about the memory leak in pre-4.0 Android as mentioned by pandre.



Related Topics



Leave a reply



Submit