Hello all,
Today I going to share the code to apply the custom fonts in resource file without any change in source file .
For this you first need to download the fonts which you want to apply on the Textview.
I have download the fonts from the site : 1001fonts
Then paste it in the assets folder of your project.
Now let's move to the coding section:
First make a attribute xml file in values folder, named : attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Now make a package for adding custom font helper classes,
First make a java class in this package,I have named it: FontHelper.java ,then copy the following code:
import java.util.HashMap;
import java.util.Map;
import com.blogdemo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FontHelper {
private static final String TAG = "TextView";
private static Map<String ,Typeface> fonts = new HashMap<String ,Typeface>();
public static Typeface getTypeFace(Context context, String fontPath) {
if (!fonts.containsKey(fontPath)) {
Typeface font = Typeface.createFromAsset(context.getAssets(), fontPath);
fonts.put(fontPath, font);
}
return fonts.get(fontPath);
}
public static void setFont(View view, Typeface font) {
if (view instanceof ViewGroup)
{
for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++) {
setFont(((ViewGroup)view).getChildAt(i), font);
}
} else if (view instanceof TextView) {
((TextView) view).setTypeface(font);
}
}
public static void setFont(Context ctx, TextView view, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setFont(ctx, view,customFont);
a.recycle();
}
public static boolean setFont(Context ctx, TextView view, String fontPath) {
boolean successful = true;
try {
Typeface tf = FontHelper.getTypeFace(ctx, fontPath);
view.setTypeface(tf);
} catch (Exception e) {
Log.e(TAG, "Error to get typeface: " + e.getMessage());
successful = false;
}
return successful;
}
}
Now again make another class in the same package I have named it : TextViewPlus.java
import java.util.HashMap;
import com.blogdemo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
private HashMap<String,Typeface> fontMap = new HashMap<String, Typeface>();
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
FontHelper.setFont(context, this, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
FontHelper.setFont(context, this, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
FontHelper.setFont(ctx, this, attrs);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
fontMap.put(asset,tf);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
That's all now let's move to the resource folder,make a xml which will contain the textviews with different fonts,I have named it : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.blogdemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<com.customtext.TextViewPlus
android:id="@+id/txt_1_mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:layout_marginTop="20dp"
android:text="@string/sample_text"
android:textSize="20dp"
android:textColor="#000000"
foo:customFont="Liima.ttf"
>
</com.customtext.TextViewPlus>
<com.customtext.TextViewPlus
android:id="@+id/txt_2_mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:layout_marginTop="40dp"
android:text="@string/sample_text"
android:textSize="20dp"
android:textColor="#FF0000"
foo:customFont="Tusj.ttf"
>
</com.customtext.TextViewPlus>
</LinearLayout>
->That's all, now use this xml and textview ,same as normal textviews. here one thing should be noticed in this xml :
xmlns:foo="http://schemas.android.com/apk/res/com.blogdemo"
you can replace com.blogdemo with your main package.
Here is the screen shot for the custom text fonts:
Today I going to share the code to apply the custom fonts in resource file without any change in source file .
For this you first need to download the fonts which you want to apply on the Textview.
I have download the fonts from the site : 1001fonts
Then paste it in the assets folder of your project.
Now let's move to the coding section:
First make a attribute xml file in values folder, named : attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
Now make a package for adding custom font helper classes,
First make a java class in this package,I have named it: FontHelper.java ,then copy the following code:
import java.util.HashMap;
import java.util.Map;
import com.blogdemo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FontHelper {
private static final String TAG = "TextView";
private static Map<String ,Typeface> fonts = new HashMap<String ,Typeface>();
public static Typeface getTypeFace(Context context, String fontPath) {
if (!fonts.containsKey(fontPath)) {
Typeface font = Typeface.createFromAsset(context.getAssets(), fontPath);
fonts.put(fontPath, font);
}
return fonts.get(fontPath);
}
public static void setFont(View view, Typeface font) {
if (view instanceof ViewGroup)
{
for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++) {
setFont(((ViewGroup)view).getChildAt(i), font);
}
} else if (view instanceof TextView) {
((TextView) view).setTypeface(font);
}
}
public static void setFont(Context ctx, TextView view, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setFont(ctx, view,customFont);
a.recycle();
}
public static boolean setFont(Context ctx, TextView view, String fontPath) {
boolean successful = true;
try {
Typeface tf = FontHelper.getTypeFace(ctx, fontPath);
view.setTypeface(tf);
} catch (Exception e) {
Log.e(TAG, "Error to get typeface: " + e.getMessage());
successful = false;
}
return successful;
}
}
import java.util.HashMap;
import com.blogdemo.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
private HashMap<String,Typeface> fontMap = new HashMap<String, Typeface>();
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
FontHelper.setFont(context, this, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
FontHelper.setFont(context, this, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
FontHelper.setFont(ctx, this, attrs);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
fontMap.put(asset,tf);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
That's all now let's move to the resource folder,make a xml which will contain the textviews with different fonts,I have named it : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/com.blogdemo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<com.customtext.TextViewPlus
android:id="@+id/txt_1_mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:layout_marginTop="20dp"
android:text="@string/sample_text"
android:textSize="20dp"
android:textColor="#000000"
foo:customFont="Liima.ttf"
>
</com.customtext.TextViewPlus>
<com.customtext.TextViewPlus
android:id="@+id/txt_2_mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:layout_marginTop="40dp"
android:text="@string/sample_text"
android:textSize="20dp"
android:textColor="#FF0000"
foo:customFont="Tusj.ttf"
>
</com.customtext.TextViewPlus>
</LinearLayout>
->That's all, now use this xml and textview ,same as normal textviews. here one thing should be noticed in this xml :
xmlns:foo="http://schemas.android.com/apk/res/com.blogdemo"
you can replace com.blogdemo with your main package.
Here is the screen shot for the custom text fonts: