I am trying to use custom font in android. I have written java code as given below.
TextView txt = (TextView) findViewById(R.id.customfont);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
txt.setTypeface(font);
where I have stored the custom font in "./assets/fonts/" folder.
But, I am getting nullpointerexception on the 3rd line.
Can anybody give me the solution for this issue? Thanks
Android supports only TTF font type not OTF. Check this link
"RuntimeException: native typeface cannot be made" when loading font
Android supports OTF as well. Your font may be corrupt.
Download delicious font from www.exljbris.com/delicious.html. It is an otf font and it works for me.
Try this
Typeface font = Typeface.createFromAsset(getAssets(), "/fonts/Molot.otf");
Related
In the android guides for Fonts in XML, they give the example of font family XML you create by adding:
android:font="#font/lobster_regular"
Which I think its the font you added to your assets/font folder. But when i try it I don't get it why it isn't able to find the font I've added. It only fins sans, serif and mono-space. Anyone have a clue what it might be the problem?
I know there are other ways to add a custom font, but due to the material calendar that I want to use, I need it to define in the styles XML, only way to customize it. I tried calligraphy lib, but does not help.
Using typeface you easily set font family
Typeface type = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
now set this typeface to your textview
tv.setTypeface(type);
Add this to your activity code:
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
tv.setTypeface(face);
Note that for this you'll have to create a fonts folder at app/src/main/assets
I added a Google font using Android Studio,
It created a 'font' folder and under it is an XML file as follow:
res -> font -> font_name.xml
I'm trying to apply it programmatically but I can'y find how to do it,
I've tried several codes but nothing worked, examples:
Typeface font = Typeface.createFromFile("font/font_name.xml");
or
Typeface font = Typeface.createFromAsset(getAssets(), "#font/font_name");
Please note,
The 'font' folder along with the 'font_name.xml' file,
Were created automatically by Android Studio.
Your help would be appreciated,
Thank you.
I am certainly not an expert, but I managed to get my Typefaces working.
Firstly, this thread contains a lot of useful info:
Custom fonts and XML layouts (Android)
Now specifically to your problem, the way I managed to get Typefaces to work is by downloading a .ttf file either somewhere online, or when using android studio and clicking more fonts, set the radio button to add font to project. That will download the .ttf to res\font\font.ttf Move it to your assets directory into fonts folder and then create the typeface like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
If you want to use your font often and don't want to bother setting it every time, a good idea might be extending a TextView or whatever class you need and using the .setTypeface() method in the custom constructor.
Just solved this myself, here is what I have put together.
Typeface font = Typeface.create("cursive", Typeface.NORMAL);
Log.e("Setup", "Font: " + font.getStyle());
signatureButton.setTypeface(font);
The log just checks if you have selected an acceptable font, it should return 0 if not. The hardest part was finding a font family that worked. The key is to go to your XML file and type "android:fontFamily=" from here a suggestion list should pop up with choices at the bottom.
To change the google font family
Typeface typeface = ResourcesCompat.getFont(this, R.font.your_font);
textView.setTypeface(typeface);
Add that to your fonts folder then use them in the XML or programmatically.
Hi am developing One Android App in which am using Custom font .TTF file.The problem is i tested with same Branded Device with Different Build Version.the Older Version Device supporting Custom Font But new version Device not taking Custom Font.
i have one suggestion add those font in assets and make your text in html thus you can add it in Web View android
Hope it will work for your case:
WebView view = new WebView(this);
view.setVerticalScrollBarEnabled(true);
view.loadUrl("file:///android_asset/demo/intro.html");
((LinearLayout)findViewById(R.id.introLayout1)).addView(view);
view.setBackgroundColor(Color.BLACK)
first download the needed the ttf file (Font style which you want ). and put that it in the assets folder in android project. then add the following code in java file..
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Eltpan-n.ttf");
txt.setTypeface(font);
hope it will help you.. its worked like a charm for me .. if you satisfied vote for my answer
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
I have added an external font in /assets directory, and manually doing setFacetype(font).
Isn't there a general way to set the whole application to use a specific font if you have added it external? Or do you have to use Android's selected fonts in order to achieve this?
You cannot use your custom fonts through to whole application in a general way.
You cannot set your custom fonts through xml files.
You have to use the Typeface functions in your code to use your custom fonts within your application.
tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "MYFONT.TTF");
tv.setTypeface(font);
This also how to use it in a textview.
For whole application go to Using a custom typeface in Android.
and go to Manish Singla answer
Typeface mTypeface = Typeface.createFromAsset(getAssets(), "YOUR FONT NAME");
textview.setTypeface(mTypeface, Typeface.NORMAL);