Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assets folder in Android Studio?

Tags:

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?

like image 879
Jojo Avatar asked Oct 07 '13 13:10

Jojo


People also ask

How do I find the assets folder in Android Studio?

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.

What is the use of assets folder in Android?

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.

What is an asset folder?

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.


2 Answers

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!

like image 113
agreeingly-ascyphous Avatar answered Sep 23 '22 21:09

agreeingly-ascyphous


Create Assets folder Right click on app->>new->>Folder->>AssetsFolder like below image

enter image description here

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); 
like image 20
kundan roy Avatar answered Sep 21 '22 21:09

kundan roy