Is It Possible To Use Fancy Fonts In Android?
Solution 1:
you should do it in this way
first create a folder named assets and create another folder within it named as fonts now paste some .ttf fonts file into it lets say you've file named as neutra_bold.ttf
now create a class that extends TextView for eg.
package com.view9.costumviews;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
publicclassNeutraBoldTextViewextendsTextView {
publicNeutraBoldTextView(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
this.setTypeface(face);
}
publicNeutraBoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
this.setTypeface(face);
}
publicNeutraBoldTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
this.setTypeface(face);
}
protectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
now to use this view you can do this in xml layout like this
<com.view9.costumviews.NeutraBoldTextView
android:id="@+id/tvViewAttachmentDescLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/text_description"
android:textColor="@color/heading_text_color"
/>
Solution 2:
Android's TextView
widget is that you couldn't specify a custom
font in the XML layout.
You just need to download the required font from the internet, and then place it in assets/fonts folder.
After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class
. First , get the reference of the text view in the code. Its syntax is given below −
TextViewtx= (TextView)findViewById(R.id.textview1);
The next thing you need to do is to call static method of Typeface class createFromAsset()
to get your custom font from assets. Its syntax is given below −
Typefacecustom_font= Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface()
method to do that. Its syntax is given below −
tx.setTypeface(custom_font);
Reference .For more details Using-custom-fonts-in-your-android-apps And Custom Fonts on Android — Extending TextView
Solution 3:
You need to place your font file in assets folder. You need to add Typeface to your textview or any other view like below:
This method is suitable for one or few views but for multiple view it will be duplicating the code.
Typeface type = Typeface.createFromAsset(getAssets(),"Kokila.ttf");
txtyour.setTypeface(type);
A better approach is to use your own font manager like this one:
publicclassQuickFontManager {
privatestatic int cacheSize=6;
privatestaticboolean debuggable=false;
privatestaticLruCache<String, Typeface> lruCache;
privateQuickFontManager(int cacheSize, boolean debuggable)
{
QuickFontManager.debuggable=debuggable;
if(lruCache==null)
{
QuickFontManager.cacheSize=cacheSize;
lruCache= newLruCache<String, Typeface>(cacheSize);
}else
{
Log.e("QuickFonts","Cache already configured, use configuration before using typeface. Application class is a good contender.");
}
}
publicstaticvoidclearCache()
{
if(lruCache!=null)
{
lruCache.evictAll();
lruCache=null;
}
}
/**
*
* @paramcontext
* @paramname
* @return A pair containing required typeface and boolean value for whether it was fetched from cache.Boolean works only for debuggable mode.
*/publicstaticPair<Typeface, Boolean> getTypeface(Context context, String name) {
if(lruCache==null)
{
lruCache= newLruCache<String, Typeface>(cacheSize);
}
Typeface typeface = lruCache.get(name);
boolean fromCache=true;
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(), name);
fromCache=false;
} catch (Exception e) {
typeface=null;
}
if (typeface == null) {
thrownewNullPointerException("Resource named " + name + " not found in assets");
} else
{
lruCache.put(name, typeface);
}
}
if(!QuickFontManager.debuggable)
{
fromCache=true; // User has not asked for debugging ,let's fool views
}
returnPair.create(typeface,fromCache);
}
publicstaticclassQuickFontBuilder
{
private int cacheSize;
privateboolean debuggable=false;
publicQuickFontBuilder()
{
}
publicQuickFontBuildersetCachesize(int cachesize)
{
this.cacheSize=cachesize;
returnthis;
}
publicQuickFontManagerbuild()
{
returnnewQuickFontManager(this.cacheSize,this.debuggable);
}
publicQuickFontBuildersetDebuggable(boolean debuggable) {
this.debuggable = debuggable;
returnthis;
}
}
}
Then create your custom textview or any other view (radio button,checkbox etc) like:
publicclassTextViewextendsandroid.widget.TextView {
private String quickfont;
publicTextView(Context context) {
super(context);
init(null, 0);
}
publicTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
publicTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
privatevoidinit(AttributeSet attrs, int defStyle) {
// Load attributesfinalTypedArraya= getContext().obtainStyledAttributes(attrs, R.styleable.TextView, defStyle, 0);
try {
quickfont = a.getString(R.styleable.TextView_quickfont);
} catch (Exception e) {
e.printStackTrace();
}finally {
a.recycle();
}
if(quickfont!=null&!isInEditMode())
{
Pair<Typeface,Boolean> pair= QuickFontManager.getTypeface(getContext(), quickfont);
Typeface typeface=pair.first;
boolean fromCache=pair.second;
if(typeface!=null)
{
setTypeface(typeface);
}
if(!fromCache)setTextColor(Color.RED);
}
// Note: This flag is required for proper typeface rendering
setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
and in your values folder create a resource file attrs.xml(if not created) and add this:
<!--for custom views--><declare-styleablename="TextView"><attrname="quickfont"format="string" /></declare-styleable>
all the hard work is done.Now you can simple use it in your xml for any view like
<com.abc.views.TextViewapp:quickfont="OpenSans-Regular_1.ttf"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@android:color/white"
/>
see that i assigned the font using app:quickfont.Where OpenSans-Regular_1.ttf is my font file in assets folder and com.abc.views.TextView is my custom textview.
Solution 4:
Download and Copy Font Files (.ttf,.ttc,.otf etc ) to Assets Folder
set Fonts is to do it programatically:
TextView tv= (TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/heartbre.ttf");
tv.setTypeface(face);
or Else Create CustomText
Class
publicclassCustomTextViewextendsTextView
{
Context context;
String ttfName;
String attrName ;
StringTAG= getClass().getName();
publicCustomTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
for (inti=0; i < attrs.getAttributeCount(); i++)
{
try
{
attrName = attrs.getAttributeName(i);
if(attrName.equals("fontFamily"))
{
this.ttfName = attrs.getAttributeValue(i);
Typefacefont= Typeface.createFromAsset(context.getAssets(), "fonts/"+this.ttfName);
setTypeface(font);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
@OverridepublicvoidsetTypeface(Typeface tf)
{
super.setTypeface(tf);
}
}
you Can set Font Family through xml for Example
<com.gm.custom_classes.CustomTextView
android:id="@+id/updater_invalid_vin_content"
android:layout_width="629dp"
android:layout_height="266dp"
android:layout_marginLeft="86dp"
android:layout_marginTop="91dp"
android:fontFamily="GMSansUI_Light.ttf"
android:gravity="left"
android:maxLines="7"
android:singleLine="false"
android:text="@string/updater_invalid_vin_content"
android:textColor="#ffffff"
android:textSize="28sp" />
Solution 5:
I did all of these things mentioned in the answers, but honestly the easiest and most convenient method for applying fonts was to use this library.
Calligraphy by Christopher Jenkins
Blog post summarising his architecture
The setup is very easy and supports all cases.
I would suggest you use this because there is no point re-inventing the wheel and the library is constantly being improved, so you can depend on it for all your other projects also!
Hope it helps! :)
Post a Comment for "Is It Possible To Use Fancy Fonts In Android?"