How to import font in Android studio - android

I am trying to change the font style in my Android App. So I copy and paste the font file (.ttf) into the assets folder. After pasting it show's File was loaded in the wrong encoding :UTF-8. How can I solve it? (attached picture below)
Note :
I already tried following ways.
a) convert file encoding to UTF-8
b) set the right file encoding in your build.gradle script.
But the problem is not solved yet. It still shows encode formats error.

I faced similar issue when I was playing with fonts. I think you are facing the same issue. In my Android Studio .tff file was linked to a text file. I changed that in the File Type option in Settings and font was working fine.
Steps:
Go to Settings
Select File Type
Search for .tff file
Deactivate linking as text (it maybe some other type in your case)
Hope this solves your issue.

Thankyou #Sreeram TP. This post may help to resolve that problem completely. I show my solution below.
After deactivating linking as text (pic1 below)
I deleted and re-pasted my font.ttf file. It then shows the following alert (pic2 below):
I selected Open matching file in associated application.
This solved my problem completely.

It is not a problem actually. You can still use the custom font like this :
TextView text = (TextView) findViewById(R.id.custom_text);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);

Related

Why does the android studio gives me an error when I add the custom font?

When I add a font in my res/font directory, The red underline happens on the font.
I tried adding font.xml, but that didn't work.
Also, In the problems tab, it say "No errors found by the IDE"
Why does this error happen?
Try this: You have to create a folder named font inside the res folder and copy your font inside the new folder.
All font names must be only: lowercase,a-z, 0-9, not contain spaces.
Now programmatically you should be able to use the font:
textView.setTypeface(ResourcesCompat.getFont(context, R.font))

I can't apply Typeface programmatically for Google font in Android Studio

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.

Custom Fonts not working in lollipop?

I have developed a handwriting app .And one of the main feature of this is that we can change the fonts from default font to 4 other custom fonts ie, to
Vicmorg (Vic Modern Cursive)
Cursive Writing 7
Dnealiancursive
Print-Regular
And this is working fine in all devices.Yesterday I updated my nexus 10 to lollipop .Now the fonts are not taken correctly, vicmorg.ttf and Dnealiancursive.ttf are now not working but Print-Regular and Cursive Writing 7 are working .No issues in other phones though. Is there a fix for this ? Please help.
re-convert your whatever fonts to .ttf from this link works for me.
http://www.freefontconverter.com/
I did it in following way:
I have updated TextView with androidx.appcompat.widget.AppCompatTextViewand it works for each device.
Example
From
<TextView
To
<androidx.appcompat.widget.AppCompatTextView
Hope it helps you.
Error on lollipoop
test files .otf run correctly but .ttf not.
my solution for files .ttf was convert to .otf
http://www.files-conversion.com/font-converter.php
or
http://www.font2web.com/
for custom font for example, inside assets folder (../assets/fonts/birdman-webfont.otf):
Typeface birdman = Typeface.createFromAsset(getAssets(), "fonts/birdman-webfont.ttf");
TextView text= (TextView) findViewById(R.id.text);
text.setTypeface(birdman);
note: if your file is .ttf and show ERROR in the convertion, change the extension to .otf manually and convert with http://www.files-conversion.com/font-converter.php

Custom ttf fonts are not showing properly in TextView on Android 4.4 KitKat

I have some text which has some bolded parts. Until KitKat this strategy (as mentioned in this post) worked perfectly well
My strings resources file:
<string name="multi_style_text">NON-BOLD TEXT \n<b>BOLD</b></string>
My application code in fragment:
txtView.setTypeface(FontUtils.getOstrichRegular(this.getActivity()));
...
public static Typeface getOstrichRegular(Context context) {
return Typeface.createFromAsset(context.getAssets(),
"fonts/ostrich_regular.ttf");
}
Currently (in KitKat), the bolded part is not shown in the custom font, the non-bolded part is shown in the custom font. In previous versions of Android, all of the text was shown in the custom font.
What gives?
So, after being frustrated by this bug, I searched around and found a solution to the problem.
In my current project we use calibri.ttf font. that was working fine up to 4.4. Once i got the update to my nexus 4, All the TextViews with Calibri font were showing "ff" instead of the entire text.
THE FIX - get an .otf (open type font) version of your font, and put in the project, works like a charm. Too bad google didn't inform the developers on this and there's very little documentation on the matter.
Apparently this is a bug in KitKat and has been fixed in an internal tree.
Put your custom font in android assets under folder name "font" or whatever you want
Try this
myTypeface = Typeface.createFromAsset(this.getAssets(),
"fonts/<<your font>>.ttf");
in onCreate() then
[use youcontroll].setTypeface(myTypeface);
Best of Luck...
I resolved the problem by converting my file.ttf to file.otf
remplace :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.ttf");
yourTextView.setTypeface(typeface);
by :
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "fonts/ostrich_regular.otf");
yourTextView.setTypeface(typeface);
FYI : the .otf format work for all android version (not only on kitkat)
After many hours searching for roboto.otf (2014 year) I understood that it was a mistake. Simply download a normal ttf font from https://www.fontsquirrel.com/fonts/roboto-2014 and copy to assets folder, then use setTypeface.
Also you may convert it to otf with any web-site.

Android imageView id not showing

I have an imageView in my main.xml file, that isn't setup with a source initially. I want to be able to dynamically change the image resource within the code. so lets so i have my imageView setup like this:
<ImageView
android:id="#+id/countryImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/CountryImageContent"/>
So i try to access within my code like this:
ImageView img = (ImageView) findViewById(R.id.countryImage);
The problem is here. Eclipse is giving me an error saying that "countryImage cannot be resolved or is not a field". A quick peek into my R.java file is showing me that the imageView isn't actually in there. Why is this happening? Thanks.
look into your drawable and layout if there is any error
if not try cleaning your project
Go to Project > clean > select your project
if not then look into your imports for
your.packagename.R remove it
Project -> Clean.
That is the only solution to this.
Also make sure that among your import statements, you do not have this : import android.R
Check this link for a detailed procedure.
Note : I am assuming that there are no errors in your layout and drawable files, as errors in them may also lead to this problem.
make sure all resource files have only lowercase letters , numbers and the underscore character ("_") .
only if there are no errors in creating the R file (in the "gen" folder) , you can use the id of it.
I think it is the problem to giving the name of #string
Please Remove all UpperCase Character from #string/CountryImageContent and replace it with small character.
R.java file doesn't support any Uppercase character. It gives the same error when you declare the file name of any resources Like :- Image, XML, #string, etc...
Just rename CountryImageContent with country_image_content. it will work perfect.
Another option to try in this case is: File --> Invalidate Caches / Restart...

Categories

Resources