custom font in Android Studio

Football-Is-My-Life picture Football-Is-My-Life · Jun 27, 2014 · Viewed 12.7k times · Source

How to create custom fonts in android studio ? Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I have searched long and hard but found nothing that helped yet. Where I wrong? I really do not know what to do. I wrote all the details below. I've tried and did not succeed. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app crashes when I place the assets folder in /main/assets. Im using this code to load my fonts.

I know that . structure for a project in Android Studio 0.5.2:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------fonts/SomeFont.ttc
|----------fonts/AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

Update

my fonte file dir : E:\AndroidProjects\ReadNews\ReadNews\src\main\assets\fonts\

end Update

my code :

 @Override
     protected void onPreExecute() {
            TextView titleTextView = (TextView) _dialog.findViewById(R.id.title_news_TextView);
            TextView messageTextView = (TextView) _dialog.findViewById(R.id.message_TextView);
            String fontPath = "fonts/Tahoma.ttf";
            Typeface tf = Typeface.createFromAsset(MainActivity.this.getResources().getAssets(), fontPath);
            titleTextView.setTypeface(tf);
            messageTextView.setTypeface(tf);
        }

error :

06-27 07:40:37.985 4206-4206/com.mmd2009.readnews E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.mmd2009.readnews, PID: 4206 java.lang.RuntimeException: native typeface cannot be made at android.graphics.Typeface.(Typeface.java:175) at android.graphics.Typeface.createFromAsset(Typeface.java:149) at com.mmd2009.readnews.MainActivity$ReadNewsAsyncTask.onPreExecute(MainActivity.java:216) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.mmd2009.readnews.MainActivity$1.onClick(MainActivity.java:70) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

my ReadNews-ReadNews.iml :

  <facet type="android" name="Android">
      <configuration>
        <option name="SELECTED_BUILD_VARIANT" value="debug" />
        <option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
        <option name="COMPILE_JAVA_TASK_NAME" value="compileDebugJava" />
        <option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
        <option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
        <option name="ALLOW_USER_CONFIGURATION" value="false" />
        <option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
        <option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
        <option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
        <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
      </configuration>
    </facet>

Answer

user3331142 picture user3331142 · Jun 27, 2014

Your font path is incorrect. You have no fontfolder. Change your font path to just "Tahoma.ttf", or create the folder inside of your asset folder.

EDIT

This appears to be a bug that happens with Android OS on occasion. Check out this link, it appears it has been addressed already. "Native typeface cannot be made" only for some people