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.
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");
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);
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);
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());
You have to place your assets folder inside "Project" folder and not in the "src" folder. You have placed your font in the "src/main/assets/robotobold.ttf" that's why it is not working. You need to place it like this "/assets/robotobold.ttf".
Typeface robo = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
My mistake was adding the line
Typeface mFont = Typeface.createFromAsset(this.getAssets(), "abc.ttf");
before onCreate()
Related
I have textview which contains a string that i want a specific part of it to be red and use a different font from the rest of the text this is what i've tried
in the onCreate method i put a different font on the whole textview
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Light.ttf");
mn.setTypeface(tf);
and after that Si is part of a for loop and when a case is matched i want to change the font
Si+="<font face='RobotoCondensed-Bold' color='#8B2121'><b>"+SiArray[0]+"</b></font>";
mn.setText(Html.fromHtml(Si));
mn.setTextSize(19.5f);
so RobotoCondensed-Bold is a font that i have in assets folder in my android project how to use that font instead of the regular Ariel....etc...etc...
I have textview. I could not show japanese characteristics. So downloaded font to Assets/fonts folder. I tried this code
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/ipag.ttf");
TextView tv=(TextView) findViewById(R.id.name);
tv.setTypeface(tf);
But application not working. What should be changed?
This code has work for me:
first declare the font:
Typeface font;
second inside the onCreate:
font = Typeface.createFromAsset(getAssets(), "myfont.ttf");
third: also inside the onCreate:
myTest = (Button) findViewById(R.id.myTest);
myTest.setTypeface(font);
My application can change the languaje and japanesse in one of those, and it works well.
This is the font used:
http://wowslider.com/styles/miso-bold-webfont.ttf.
by the way, the font is inside the Assets folder.
I am trying to change typeface in a Textview which shows a chinese text.
My code is:
chinesetext = (TextView)findViewById(R.id.textChinese);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/DFXSM1B.ttf");
chinesetext.setTypeface(tf);
DFXSM1B.ttf is in my assets/fonts folder.
I am unable to see the textview typeface changed.
How can I change to a custom typeface in a chinese text ?
I am using paint to set typeface, it dosen't work for chinese text...
I used the font family is 思源宋体 Medium
and my code like this: (that is very simple)
var typeface = Typeface.createFromAsset(context.assets, "fonts/font1.otf")
paint.typeface = typeface
All the files in asset folder must have lower-cased names.
Rename the file in lower case (dfxsm1b.ttf) and it must work.
I am doing like this but it get force close. I want to change textview font.
TextView mWeddingDataandTime=(TextView)findViewById(R.id.wedding_weddingtime_txt);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/CURLZ_.otf");
mWeddingDataandTime.setTypeface(face);
I am using .otf file. It is in assets folder. Any idea?
Try this:
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
Note that file extension is "ttf" search in google for any font for download in this extension
for example:
http://www.creamundo.com/
In code you are using fonts/CURLZ_.otf as path but you say that font file is directly inside assets so your code should be
Typeface face=Typeface.createFromAsset(getAssets(),"CURLZ_.otf");
if there is no fonts folder inside assets folder.
I didn't test it but I think it may help you:
TextView mWeddingDataandTime=(TextView)findViewById(R.id.wedding_weddingtime_txt);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/CURLZ_");
mWeddingDataandTime.setTypeface(face);
If your font is only in asset folder not in asser/fonts forlder than use this
Typeface face=Typeface.createFromAsset(getAssets(),"CURLZ_.otf");
Below is code which I used in my project & it's working... You might need to do some changes
Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"segoeuil.ttf");
TextView loantext = (TextView)findViewById(R.id.loantext);
length1.setTypeface(myTypeface);
1) Put font in assets folder instead of fonts folders
2) instead of getAssests() in you code use "this.getAssets()" or "youractivity.this.getAssets()
Thank You
Better to make use of the styles and themes and inbuilt fonts for performance related and size of the app related issues Styles & Themes
Hi want to change the font type to atext according to spinner selection like arial balck,Thlist etc.any one have idea ? suggest me
You should change the typefaces of your text views. You can specify a variable:
Typeface typeface = Typeface.DEFAULT;
Then when the user chooses another font you reinitialize this variable, using the built-in typefaces, or loading your own from .ttf files, that you can place in assets/fonts directory. Loading own fonts can be done with this call:
typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/font.ttf");
This should do it.