Last active
August 29, 2015 14:05
-
-
Save layerlre/793f9386a38c19e30a33 to your computer and use it in GitHub Desktop.
Custom font textview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.layer.view; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class TextViewRoboto extends TextView { | |
public TextViewRoboto(Context context) { | |
super(context); | |
setFont(); | |
} | |
public TextViewRoboto(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setFont(); | |
} | |
public TextViewRoboto(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
setFont(); | |
} | |
private void setFont() { | |
int style = Typeface.NORMAL; | |
try { | |
style = getTypeface().getStyle(); | |
} catch (Exception e) { | |
// TODO: handle exception | |
} | |
Typeface face; | |
if (style == Typeface.NORMAL) { | |
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"); | |
} else if (style == Typeface.BOLD) { | |
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Black.ttf"); | |
} else if (style == Typeface.ITALIC) { | |
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"); | |
} else { | |
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"); | |
} | |
this.setTypeface(face); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment