how to create an object of the class, and provide value to constructor like AttributeSet etc. and also setText("")
public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(getTextSize());
}
#Override
public void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
// ..........
}
}
// how to use in java
CustomTextView cTV = new CustomTextView (...........)??
cTV.setText("how to create like that");
first of all you need to define the class.
public class CustomFontTextView extends AppCompatTextView {
private static final String CUSTOM_FONT = "cfont";
private static final String FONT_PATH = "fonts/";
private String ttfName;
private Typeface font;
private CharSequence text;
private BufferType type;
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
this.ttfName = attrs.getAttributeValue(AppConstants.NAMESPACE, CUSTOM_FONT);
if (ttfName.startsWith("#string/") || ttfName.startsWith("#")) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
this.ttfName = ta.getString(R.styleable.CustomFontTextView_cfont);
ta.recycle();
}
this.setPaintFlags(this.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
try {
font = Typeface.createFromAsset(context.getAssets(), FONT_PATH + ttfName);
setTypeface(font);
} catch (Exception e) {
e.printStackTrace();
font = Typeface.defaultFromStyle(Typeface.NORMAL);
setTypeface(font);
}
setText(text, type);
}
#Override
public void setText(CharSequence text, BufferType type) {
try {
this.text = text;
this.type = type;
if (font == null)
return;
CustomFontStyling customFontStyling = new CustomFontStyling(getContext(), font);
super.setText(customFontStyling.getCustomText(text.toString()), type);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
After you need to add the styleable in your attrs.xml
<declare-styleable name="CustomFontTextView">
<attr name="cfont" format="string" />
</declare-styleable>
And finally on your xml layout
<YOURAPPPACKAGE.CustomFontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cfont="#string/open_sans_regular" />
I just create Constructor for CustomTextView Class
public CustomTextView(Context context) {
this(context, null);
}
Now we can easily access any properties of CustomTextView Class.
Related
I am trying to create a TextView with custom font-family. I have Created one which supports the normal font. But I am unable to implement bold/italic styling on the custom TextView.
This is my Custom TextView Class
public class KTextView extends TextView {
public KTextView(Context context) {
super(context);
TypefaceHelper.setTypeface(context, this);
}
public KTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypefaceHelper.setTypeface(context, this);
}
public KTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypefaceHelper.setTypeface(context, this);
}
}
This is the TypefaceHelper Class
public class TypefaceHelper {
private static final String FONT = "AvenirNext-Regular.otf";
private static Typeface typeface = null;
public static void setTypeface(Context context, TextView textView) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), FONT);
}
textView.setTypeface(typeface);
}
Can anyone suggest a way to implement Bold and Italic Styling.
try this:
textview.setTypeface(typeface, Typeface.BOLD);
you can use also ITALIC and BOLD_ITALIC
for custom textview use:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
} else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
}
}
}
I need to style the text for textview as per the below image
The font being used is CARTER ONE font
This is my textview class
public class CustomTextViewCarter extends TextView {
private static Typeface mTypeface;
public CustomTextViewCarter(final Context context) {
this(context, null);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getResources().getAssets(), "carter_one.ttf");
}
setTypeface(mTypeface);
}
}
}
But the the same kind of text doesn't appear. How can it be done?
Use this code for show custom TextView
Step-1:
CustomTextView.java
public class CustomTextView extends TextView {
private static final String TAG = "TextView";
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTV);
String customFont = a.getString(R.styleable.CustomTV_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
Step-2:
After that write below code in your styles.xml file
<declare-styleable name="CustomTV">
<attr name="customFont" format="string"></attr>
</declare-styleable>
Step-3:
Use CustomTextView in your xml file
Here com.app.demo is my package name.. use your package name instead of that,
<com.app.demo.CustomTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textColor="#color/text"
android:textStyle="bold"
app:customFont="carter_one.ttf" />
Try this.
You can set ttf by making your own textview implementation.
It will works above API 15 (Ice cream sandwich).
https://stackoverflow.com/a/5185587/850347
<com.lht.ui.MyTextView
android:text="Hello friends"
lht:ttf_name="ITCBLKAD.TTF"
/>
I am currently implementing Roboto font within my project. For some fragments, there are a lot of TextView's. I am creating a custom View that exends TextView to implement custom fonts. Is there a better way to load the fonts without increasing the onCreate times?
Extends TextView
public class TextViewFont extends TextView {
public TextViewFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public TextViewFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public TextViewFont(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextViewFont);
String fontName = a.getString(R.styleable.TextViewFont_fontName);
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
XML
<com.eugene.fithealthmaingit.Custom.TextViewFont
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="#string/dinner"
android:textColor="#color/text_color"
android:textSize="16sp"
app:fontName="Roboto-Regular.ttf"/>
Example of how many TextView's
This is the library that saved my life Calligraphy. It's really nice and easy to use.
Typeface.createfromassets is a time taking process. You should declare typeface as static varaiable in class and just use it in constructor.
But here you are loading fonts in every textview's constructor.
If you having multiple fonts, have all tytypeface as static and use it appropriately.
UPDATE CODE:
public class TextViewFont extends TextView {
public static Typeface typeface1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName1");
public static Typeface typeface2 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName2");
public static Typeface typeface3 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName3");
public static Typeface typeface4 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName4");
public static Typeface typeface5 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName5");
public TextViewFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public TextViewFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public TextViewFont(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextViewFont);
String fontName = a.getString(R.styleable.TextViewFont_fontName);
if (fontName != null) {
setTypeface(getTypeFace(fontName));
}
a.recycle();
}
}
public Typeface getTypeFace(String fontName){
if(fontName.equals("fontName1")){
return typeface1;
}else if(fontName.equals("fontName2")){
return typeface2;
}else if(fontName.equals("fontName3")){
return typeface3;
}else if(fontName.equals("fontName4")){
return typeface4;
}else if(fontName.equals("fontName5")){
return typeface5;
}
}
}
}
Try using an instance singleton or applicationsingleton. See if this works. So you can just call TextLover.get(context).getFont(id). It will create and cache it on the fly. This way your other views can also reuse the font cache. eg. buttons
class TextLover {
private static TextLover singleton;
private final Context context;
private final SparseArray<Typeface> faces = new SparseArray<Typeface>();
public TextLover get(Context context) {
if (singleton == null) {
singleton = new TextLover(context);
}
return singleton;
}
private TextLover(Context context) {
this.context = context;
}
private static final String[] fonts = {
"fonts/fontName1",
"fonts/fontName2",
"fonts/fontName3",
"fonts/fontName4",
"fonts/fontName5",
...
"fonts/fontName100"
}
// NOTE you need a mapping of ids to each asset font in fonts[]
public Typeface getFont(int id) {
Typeface font = faces.get(id);
if (font == null) {
font = Typeface.createFromAsset(context.getAssets(), fonts[id]);
faces.append(id, font);
}
return font;
}
}
i'm wrote simple widget as textview with some attributes and i want to set font for that in extended TextView class, how to do this action and i can be have this ability?
atributes:
<resources>
<declare-styleable name="TextViewStyle">
<attr name="selected_background" format="integer" />
<attr name="font" format="string" />
</declare-styleable>
</resources>
custom textview widget:
public class TextViewStyle extends TextView{
public TextViewStyle(Context context) {
super(context);
}
public TextViewStyle(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewStyle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextViewStyle, defStyle, 0);
a.recycle();
}
}
simple UI widget into xml:
<ir.jaziire.widgets.TextViewStyle
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/topic"
android:textColor="#000"
android:textSize="14dp"
android:gravity="right"
/>
in this widget i want to set app:font="" to set any font from asset
CustomTextView:
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
import com.androidhub.R;
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
try {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.font, defStyle, 0);
String str = a.getString(R.styleable.font_fonttype);
a.recycle();
switch (Integer.parseInt(str)) {
case 0:
str = "fonts/Trebuchet_MS.ttf";
break;
default:
break;
}
setTypeface(FontManager.getInstance(getContext()).loadFont(str));
} catch (Exception e) {
e.printStackTrace();
}
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
#SuppressWarnings("unused")
private void internalInit(Context context, AttributeSet attrs) {
}
FontManager:
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Typeface;
public class FontManager {
private Map<String, Typeface> fontCache = new HashMap<String, Typeface>();
private static FontManager instance = null;
private Context mContext;
private FontManager(Context mContext2) {
mContext = mContext2;
}
public synchronized static FontManager getInstance(Context mContext) {
if (instance == null) {
instance = new FontManager(mContext);
}
return instance;
}
public Typeface loadFont(String font) {
if (false == fontCache.containsKey(font)) {
fontCache.put(font,
Typeface.createFromAsset(mContext.getAssets(), font));
}
return fontCache.get(font);
}
}
in attrs.xml file:
<declare-styleable name="font">
<attr name="fonttype">
<enum name="trebuchet_ms" value="0" />
</attr>
</declare-styleable>
Use your CustomTextView in your xml as:
declare this in top of your xml
xmlns:custom="http://schemas.android.com/apk/res/com.androidhub"
And you can use your CustomTextView as :
<com.utils.CustomTextView
android:id="#+id/loadMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/custom_button_selector"
android:clickable="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:padding="10dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/load_map"
android:textColor="#color/home_buttons_selector"
android:textSize="16sp"
custom:fonttype="trebuchet_ms" />
Make sure your are placing your font in assests-->fonts-->YourFont
com.androidhub is my package name.
I used this in my project it may help You.
TextViewEx.java
public class TextViewEx extends TextView {
public enum TextStyle {BOLD,LIGHT,REGULAR,SEMIBOLD,EXOREGULAR,BOLDLARGE};
TypedArray Canvasattrs=null;
int CurvatureDegree;
boolean isCurvature = false;
String direction;
String RVal;
public TextViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TextViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs){
if (!this.isInEditMode()) { // used for preview while designing.
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView_TypeFace)
Typeface font = null;
String Type = a.getString(R.styleable.TextView_TypeFace_TypeFace);
if(Type == null){
UserTypeFace.SetRegular(this); //Set Default Font if font is not defined in xml
return;
}
setStyle(Type);
} else {
setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
}
}
private void setStyle(String style){
TextStyle value = TextStyle.valueOf(style); //convert String to ENUM
switch (value){
case BOLD:UserTypeFace.SetBold(this);
break;
case LIGHT:UserTypeFace.Setlight(this);
break;
case REGULAR:UserTypeFace.Setthin(this);
break;
case SEMIBOLD:UserTypeFace.SetSEMIBOLD(this);
break;
case EXOREGULAR:UserTypeFace.SetRegular(this);
break;
case BOLDLARGE:UserTypeFace.SetBoldLarge(this);
break;
}
}
}
UserTypeFace.java
public class UserTypeFace {
public static final String BOLD;
public static final String LIGHT;
public static final String REGULAR;
public static final String EXOREGULAR;
public static final String BOLDLARGE;
public static final String SEMIBOLD;
static {
REGULAR="fonts/JosefinSans-Bold.ttf";
LIGHT="fonts/JosefinSans-Bold.ttf";
BOLD="fonts/JosefinSans-Bold.ttf";
SEMIBOLD="fonts/JosefinSans-Bold.ttf";
BOLDLARGE="fonts/JosefinSans-Bold.ttf";
EXOREGULAR="fonts/Exo2-Regular.ttf";
}
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
private static Typeface getTypeFace(Context context, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface typeFace = Typeface.createFromAsset(
context.getAssets(), assetPath);
cache.put(assetPath, typeFace);
} catch (Exception e) {
Log.e("TypeFaces", "Typeface not loaded.");
return null;
}
}
return cache.get(assetPath);
}
}
public static void Setthin(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),EXOREGULAR), Typeface.NORMAL);
}
public static void Setlight(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),LIGHT), Typeface.NORMAL);
}
public static void SetBold(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),BOLD), Typeface.NORMAL);
}
public static void SetSEMIBOLD(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),SEMIBOLD), Typeface.NORMAL);
}
public static void SetRegular(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),EXOREGULAR), Typeface.NORMAL);
}
public static void SetBoldLarge(TextView obj){
obj.setTypeface(getTypeFace(obj.getContext(),BOLDLARGE), Typeface.BOLD);
}
public static Typeface getRegular(View obj){
return getTypeFace(obj.getContext(),EXOREGULAR);
}
}
Add this in attrs.xml
<resources>
<declare-styleable name="TextView_TypeFace">
<attr name="TypeFace" format="reference|string" localization="suggested" />
</declare-styleable>
</resources>
And You Will be adding this in layout file like
<pakage.name.TextViewEx
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:textColor="#282828"
attrs:TypeFace="#string/Bold"
android:textSize="30sp"
android:text=" Please Login "
android:id="#+id/txtlogin" />
Use the following code:
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) {
super(context);
createFont();
}
public MyTextView(Context context, AttributeSet attr, int defStyle) {
super(context, attr, defStyle);
createFont();
}
public MyTextView(Context context, AttributeSet attr) {
super(context, attr);
createFont();
}
private void createFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "SHRUTI.TTF");
setTypeface(font);
}
}
I am trying to implement a custom typeface on an EditText. Does anyone have a better approach as opposed to what I'm currently doing?
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);
Because I have lots of EditTexts...
public class CEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CEditText(Context context) {
super(context);
this.context=context;
init();
}
public CEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
this.attrs=attrs;
this.defStyle=defStyle;
init();
}
private void init() {
Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf);
}
call this class in XML as follows
<yourpackagename.CEditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
</yourpackagename.CEditText>
Create a new class that extends EditText like
public class CustomEditTextNormal extends EditText
{
public CustomEditTextNormal(Context context)
{
super(context);
init(context);
}
public CustomEditTextNormal(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public CustomEditTextNormal(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
}
public void init(Context context)
{
try
{
Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");
setTypeface(mSearchAndSend.HelveticaLight);
}
catch (Exception e)
{
Logger.LogError(e);
}
}
}
and include it on your XML like
<com.package.name.CustomEditText/>
How about create a new class that inherits EditText and sets the typeface you want and then instantiate the new class in the xml?
Create a new class which inherits from EditText, and then override the public void setTypeface(Typeface tf, int style) method and add your own font.
You need to make one method in Common class Like,
public void setExternalFonts(EdiText tv) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/myfont.ttf");
tv.setTypeface(tf);
}
Now call this method as
yourClassName.setExternalFonts(yourEditText);
Create a new class which inherits from EditText, and then override the public void setTypeface(Typeface tf, int style) method and add your own font.
And use in your activity like this
FontLoader.setQuickSandTypeface(YourEditText) in your activity
try this one
public void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "roboto_thin.ttf"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this. Also useful for Buttons, TextViews.. whatever!
<your.namespace.app.FontEditText
app:font="montserrat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
How is possible? This way!
public class FontEditText extends EditText {
public FontEditText(Context context) {
this( context, null );
}
public FontEditText(Context context, AttributeSet attrs) {
this( context, attrs, 0 );
init( context, attrs );
}
public FontEditText(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
init( context, attrs );
}
public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super( context, attrs, defStyleAttr, defStyleRes );
init( context, attrs );
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.Fonts );
if ( ta != null ) {
String fontAsset = ta.getString( R.styleable.Fonts_font );
if ( !StringUtils.isEmpty( fontAsset ) ) {
int type = Integer.parseInt( fontAsset );
Typeface typeFace = FontManager.getInstance( context ).getByType( type );
ta.recycle();
super.setTypeface( typeFace );
}
}
}
}
public class FontManager {
private static FontManager Instance;
private Context context;
private Typeface robotoCondensedBold;
private Typeface robotoCondensed;
private Typeface robotoLight;
private Typeface kronica;
private Typeface montserrat;
private Typeface montserratLight;
private Typeface keepCalmMedium;
private FontManager(Context context) {
this.context = context;
this.robotoCondensedBold = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Bold.ttf" );
this.robotoCondensed = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Regular.ttf" );
this.robotoLight = Typeface.createFromAsset( context.getAssets(), "fonts/Roboto-Light.ttf" );
this.kronica = Typeface.createFromAsset( context.getAssets(), "fonts/kronika.ttf" );
this.montserrat = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Regular.ttf" );
this.montserratLight = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Light.ttf" );
this.keepCalmMedium = Typeface.createFromAsset( context.getAssets(), "fonts/KeepCalmMedium.ttf" );
}
public synchronized static FontManager getInstance(Context context) {
if ( Instance == null )
Instance = new FontManager( context );
return Instance;
}
public Typeface getByType(int type) {
switch ( type ) {
case 0:
return FontManager.getInstance( context ).getRobotoCondensedBold();
case 1:
return FontManager.getInstance( context ).getRobotoLight();
case 2:
return FontManager.getInstance( context ).getKronica();
case 3:
return FontManager.getInstance( context ).getRobotoCondensed();
case 4:
return FontManager.getInstance( context ).getMontserrat();
case 5:
return FontManager.getInstance( context ).getMontserratLight();
case 6:
return FontManager.getInstance( context ).getKeepCalmMedium();
default:
return Typeface.DEFAULT;
}
}
public Typeface getRobotoCondensedBold() {
return robotoCondensedBold;
}
public Typeface getKronica() {
return kronica;
}
public Typeface getRobotoCondensed() {
return robotoCondensed;
}
public Typeface getRobotoLight() {
return robotoLight;
}
public Typeface getMontserrat() {
return montserrat;
}
public Typeface getMontserratLight() {
return montserratLight;
}
public Typeface getKeepCalmMedium() {
return keepCalmMedium;
}
In addition, a font_attrs.xml in your res folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Fonts">
<attr name="font" format="enum">
<enum name="robotoCondensedBold" value="0"/>
<enum name="robotoLight" value="1"/>
<enum name="kronica" value="2"/>
<enum name="robotoCondensed" value="3"/>
<enum name="montserrat" value="4"/>
<enum name="montserratLight" value="5"/>
<enum name="keepCalmMedium" value="6"/>
</attr>
</declare-styleable>
</resources>
Note that you only need to modify the FontManager and the font_attrs.xml to customize your fonts!