Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. 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 src/main. Im using this code to load my fonts.
Typeface fontRegular = Typeface.createFromAsset(getAssets(), "fonts/DroidSans.ttf"); Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/DroidSans-Bold.ttf"); myDeviceModelTxt.setTypeface(fontRegular);
What am I doing wrong?
Android App Development for Beginners Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.
Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.
Global Asset Folder Assets are non-post files in the source folder, such as images, CSS or JavaScript files. For instance, If you are only going to have a few images in the Hexo project, then the easiest way is to keep them in a source/images directory. Then, you can access them using something like ! [](/images/image.
I am not sure if there has been any bug fixes since this was asked, but I am using the current structure for a project in Android Studio 0.5.2:
root-module |--.idea |--app |----build |----src |------main |--------assets |----------SomeFont.ttc |----------AnotherFont.otf |--------java |----------source code here |--------res |------AndroidManifest.xml |----build.gradle
And then obtain it by calling
Typeface.createFromAsset(mContext.getResources().getAssets(), "SomeFont.ttc");
Word of caution though, there is a bug (https://code.google.com/p/android/issues/detail?id=9904) that prevents typefaces from being garbage collected properly. Use a singleton!
Create Assets folder Right click on app->>new->>Folder->>AssetsFolder like below image
Put your font inside this folder by just copy and paste. and use below code for example..
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "YourFontName.ttf"); setTypeface(tf);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With