Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import external font/ typeface in ANDROID STUDIO?

I want to know how do I use an external font in Android Studio as there is no Assets folder. I have look for a helpful turtorial on internet but they all pretend to use Assets folder.

I created an asset folder myself in src/main but Android Studio doesnt recognie getAssets().

like image 988
S Zain Bukhari Avatar asked Jun 24 '14 05:06

S Zain Bukhari


People also ask

How do I use OTF files on Android?

To change font styles in GO Launcher, copy the TTF or OTF font files on your phone. Long press on the home screen and select GO Settings. Choose Font–>Select Font. Pick the font you want or tap Scan to add files stored on your device.


3 Answers

Put your font files (e.g customfont.tff and customfont_bold.tff) under the folder app>src>res>font> (Note: If it is not present, create font folder)

Additionally, create a file named fonts.xml inside the same folder with the following content:

  <font-family xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

        <font
                app:fontStyle="normal"
                app:fontWeight="700"
                app:font="@font/customfont"/>

        <font
                app:fontStyle="normal"
                app:fontWeight="700"
                app:font="@font/customfont_bold"/>
  </font-family>

Then, edit the file app>src>res>values>styles.xml to apply a default font for the entire app.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily">@font/customfont</item>
</style>

If you want to change the font of an individual UI element:

<TextView
    android:fontFamily="@font/customfont"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textColor="@color/black"
    android:textSize="18sp"
    android:text="Some text"
    />

NOTE: This method is valid for API Level 16+ (For more info: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)

like image 135
hanilozmen Avatar answered Sep 28 '22 07:09

hanilozmen


If you have custom font then use following code:

TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);

Also place your font file in assets/fonts folder and follow the instructions from here.

NOTE: You have to make asset folder by yourself

like image 32
Namrata Avatar answered Sep 28 '22 08:09

Namrata


Go on your Project: app-->src-->main

Create a assets folder like this:

|assets

    |-----------------fonts

        |-------------------font.ttf

|java

|res

AndroidManifest.xml

and then use

     Typeface face=Typeface.createFromAsset(getAssets(),"fonts/digital.ttf");
     txtV.setTypeface(face);
like image 43
MD Furkanul Islam Avatar answered Sep 28 '22 07:09

MD Furkanul Islam