Skip to content

Instantly share code, notes, and snippets.

@tungphan-agilityio
Forked from Johnyoat/MyApp.kt
Created September 24, 2019 17:28
Show Gist options
  • Save tungphan-agilityio/1a202000fb23965cc8d3cf8f66d0dd2c to your computer and use it in GitHub Desktop.
Save tungphan-agilityio/1a202000fb23965cc8d3cf8f66d0dd2c to your computer and use it in GitHub Desktop.

Revisions

  1. @Johnyoat Johnyoat revised this gist Jun 13, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions TypefaceUtil.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    import android.content.Context
    import android.graphics.Typeface
    import timber.log.Timber

    class TypefaceUtil{
    fun overridefonts(context: Context, defaultFontToOverride:String, customFontFileNameInAssets:String){
    try {
  2. @Johnyoat Johnyoat created this gist Jun 13, 2019.
    7 changes: 7 additions & 0 deletions MyApp.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class MyApp : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val typefaceUtil = TypefaceUtil()
    typefaceUtil.overridefonts(this,"SERIF","fonts/roboto.ttf")
    }
    }
    12 changes: 12 additions & 0 deletions TypefaceUtil.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    class TypefaceUtil{
    fun overridefonts(context: Context, defaultFontToOverride:String, customFontFileNameInAssets:String){
    try {
    val customTypeface = Typeface.createFromAsset(context.assets,customFontFileNameInAssets)
    val defaultTypefaceField = Typeface::class.java.getDeclaredField(defaultFontToOverride)
    defaultTypefaceField.isAccessible = true
    defaultTypefaceField.set(null,customTypeface)
    }catch (e:Exception){
    Timber.e("Cannot set font $customFontFileNameInAssets instead of $defaultFontToOverride")
    }
    }
    }
    10 changes: 10 additions & 0 deletions themes.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <!-- you should set typeface which you want to override with TypefaceUtil -->
    <item name="android:typeface">serif</item>
    </style>

    </resources>