Created
December 18, 2016 18:53
-
-
Save omerucel/bdce2ef846582ecdfb7734498e33a390 to your computer and use it in GitHub Desktop.
Custom TextView with font support
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="TextView"> | |
<attr name="fontName" format="enum"> | |
<enum name="NotoSans_Bold" value="0" /> | |
<enum name="NotoSans_BoldItalic" value="1" /> | |
<enum name="NotoSans_Italic" value="2" /> | |
<enum name="NotoSans_Regular" value="3" /> | |
<enum name="NotoSansUI_Bold" value="4" /> | |
<enum name="NotoSansUI_BoldItalic" value="5" /> | |
<enum name="NotoSansUI_Italic" value="6" /> | |
<enum name="NotoSansUI_Regular" value="7" /> | |
</attr> | |
</declare-styleable> | |
</resources> |
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
<resources> | |
<string name="NotoSans_Bold">NotoSans/Bold.ttf</string> | |
<string name="NotoSans_BoldItalic">NotoSans/BoldItalic.ttf</string> | |
<string name="NotoSans_Italic">NotoSans/Italic.ttf</string> | |
<string name="NotoSans_Regular">NotoSans/Regular.ttf</string> | |
<string name="NotoSansUI_Bold">NotoSans/UI-Bold.ttf</string> | |
<string name="NotoSansUI_BoldItalic">NotoSans/UI-BoldItalic.ttf</string> | |
<string name="NotoSansUI_Italic">NotoSans/UI-Italic.ttf</string> | |
<string name="NotoSansUI_Regular">NotoSans/UI-Regular.ttf</string> | |
</resources> |
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.omerucel.widget; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Typeface; | |
import android.os.Build; | |
import android.support.annotation.RequiresApi; | |
import android.util.AttributeSet; | |
public class TextView extends android.widget.TextView { | |
public TextView(Context context) { | |
super(context); | |
} | |
public TextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs); | |
} | |
public TextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(attrs); | |
} | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(attrs); | |
} | |
protected void init(AttributeSet attributeSet) { | |
readAttributes(attributeSet); | |
} | |
protected void readAttributes(AttributeSet attributeSet) { | |
TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.TextView); | |
Integer fontType = typedArray.getInteger(R.styleable.TextView_fontName, R.string.NotoSans_Regular); | |
switch (fontType) { | |
case 0: | |
setFont(R.string.NotoSans_Bold); | |
break; | |
case 1: | |
setFont(R.string.NotoSans_BoldItalic); | |
break; | |
case 2: | |
setFont(R.string.NotoSans_Italic); | |
break; | |
case 3: | |
setFont(R.string.NotoSans_Regular); | |
break; | |
case 4: | |
setFont(R.string.NotoSansUI_Bold); | |
break; | |
case 5: | |
setFont(R.string.NotoSansUI_BoldItalic); | |
break; | |
case 6: | |
setFont(R.string.NotoSansUI_Italic); | |
break; | |
case 7: | |
setFont(R.string.NotoSansUI_Regular); | |
break; | |
} | |
typedArray.recycle(); | |
} | |
protected void setFont(int resId) { | |
setTypeface(Typeface.createFromAsset(getContext().getAssets(), getResources().getString(resId))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment