Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Typeface createFromAsset

I Have a Custom View which draws text onto the Canvas. I want to change the font to a font stored in the assets folder.

I am using Android Studio so I created a folder src/main/assets and placed my ttf files in there.

Paint txt = new Paint()
Typeface font = Typeface.createFromAsset(getAssets(), "robotobold.ttf");
txt.setTypeface(font);

Problem is Android Studio doesn't recognize getAssets() inside my Custom View, however, it recognizes it inside my Activity. I have tried passing Typeface through from my Activity but when I do it it doesn't change the font.

like image 702
Caleb Bramwell Avatar asked Aug 26 '13 04:08

Caleb Bramwell


People also ask

What typeface does Android use?

Roboto (/roʊˈbɒt. oʊ/) is a neo-grotesque sans-serif typeface family developed by Google as the system font for its mobile operating system Android, and released in 2011 for Android 4.0 "Ice Cream Sandwich".

What a typeface means?

A typeface is the design of lettering that can include variations in size, weight (e.g. bold), slope (e.g. italic), width (e.g. condensed), and so on. Each of these variations of the typeface is a font.

What is the property that is used for formatting font in Android Studio?

Use the textStyle Property to add bold, italic, or bold and italic options to the text (in the layout XML the android:textStyle attribute). The default value is normal, the value it takes if no textStyle is set.


4 Answers

You can use your View's getContext() method to get the current Context, then use it to get the assets:

Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robotobold.ttf"); 
like image 173
Phil Avatar answered Nov 04 '22 05:11

Phil


First of all, you have to keep your assets folder inside your project and not inside src/main.. And then, create a folder called fonts inside assets. then, put the specific font typeface ttf files inside it.You can use the font typeface in coding like:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/filename.ttf"); textview.setTypeface(type); 
like image 43
Kavin Avatar answered Nov 04 '22 04:11

Kavin


created a folder src/main/assets and placed font files in there.

in Activity

Typeface font = Typeface.createFromAsset(getAssets(),  "Mukta-Regular.ttf");
tv.setTypeface(font);

in Fragment

Typeface.createFromAsset(getActivity().getAssets(), "Mukta-Regular.ttf");
tv.setTypeface(font);
like image 37
Sudhir Singh Avatar answered Nov 04 '22 06:11

Sudhir Singh


In order to reuse typefaces in my projects I create a class full of typeface methods, this way I dont have to create a new typeface every time.

I call the class FontClass and in the class there is a method for each typeface I need to use e.g:

public static Typeface getOpenSansRegular(Context c){
    return Typeface.createFromAsset(c.getAssets(), "OpenSans-Light.ttf");
}

Then I can use them like so:

TextView text = (TextView) findViewById(R.id.textview);
text.setTypeface(FontClass.getOpenSansRegular(getApplicationContext());
like image 26
MichaelStoddart Avatar answered Nov 04 '22 04:11

MichaelStoddart