- Download Permanent Marker font from random internet site: https://www.fontsquirrel.com/fonts/permanent-marker
- Copy the PermanentMarker.ttf file into
assets
directory (should be the same level asres
directory NOT insideres
directory). You may have to create theassets
directory yourself in Finder. - The
attrs.xml
file below should be in/res/values/
Last active
May 1, 2016 19:46
-
-
Save rajohns08/6c4acf0b5c829d175e6ea55c54fd02ee to your computer and use it in GitHub Desktop.
Android - Custom view attributes with a custom font
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"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.your.package.CustomFontTextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/take_a_photo" | |
android:textSize="40sp" | |
app:font="PermanentMarker" /> | |
</LinearLayout> |
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="CustomFontTextView"> | |
<attr name="font" format="string"/> | |
</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
public class CustomFontTextView extends TextView { | |
public CustomFontTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, 0, 0); | |
try { | |
String fontName = ta.getString(R.styleable.CustomFontTextView_text_font); | |
this.setTypeface(Font.fromString(context, fontName)); | |
} finally { | |
ta.recycle(); | |
} | |
} | |
} |
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
public class Font { | |
public static Typeface fromString(Context context, String fontName) { | |
if (fontName == null) { | |
return Typeface.DEFAULT; | |
} | |
Typeface font = Typeface.createFromAsset(context.getApplicationContext().getAssets(), fontName + ".ttf"); | |
return font == null ? Typeface.DEFAULT : font; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment