Custom font not showing on device - android

I am trying to add a custom font following the "Fonts in XML" tutorial on this site. I followed the tutorial to the letter and checked many times to find something I missed but I just can't see it. The font I added is a TTF file.
In the designer, I can select my font family and the text in the TextView changes to the custom font I added. However, when I run the app on my device, the text is defaulted to the regular font. This doesn't happen with the fonts included in Android Studio.
Additional question: when I tried yet another custom font, the text in the TextView changed to the custom font, but the text itself (the content) also changed to some gibberish. Is this an indication of a bad font or something else?
sv_regular.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/sv_font_regular" />
</font-family>
TextView
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/sv_regular"
android:text="#string/app_name" />

You can do this programmatically
Add external fonts in your assets folder
1. Go to the (project folder)
2. Then app>src>main
3. Create folder 'assets>fonts' into the main folder.
4. Put your 'abc.ttf' into the fonts folder.
TextView tx = (TextView)findViewById(R.id.textview1);
Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/abc.ttf");
tx.setTypeface(custom_font);

I have encountered such a problem when the font is displayed in Android Studio but does not work on a physical device.
So if you want a custom font from Android Studio to be displayed on your phone screen, you need to add this font to your project as follows:
Open res folder.
Create Android Resource Directory. Resource type - font. You can also name your new folder font.
Then simply drag and drop your ttf font into the font folder. Important: The font name must not contain prohibited characters, and all letters in the name must be lowercase. The name of my font in this example: kopenhagen
(you may have a different font name).
Open your xml file, find the text in which you want to change the font, and just add the following:
android:fontFamily="#font/kopenhagen"
*Instead of the word kopenhagen, use the name of your font

Does your font exactly support the language of the text? This problem occurs when the font does not support certain characters, such as Cyrillic. And one more: try use sv_font_regular in TextView
android:fontFamily="#font/sv_font_regular"
Example

Related

Canvas drawText (font) import Times font?

I'm trying to use Times as font for the drawText()-method of the Canvas element. However couldn't find a solution yet.
There is the possibility to set fonts like 'sans-serif' or 'casual' by using following code:
paint.setTypeface(Typeface.create("casual",Typeface.NORMAL));
However trying to use Times or Arial etc. doesn't work.
Do I have to import these fonts first by myself?
Would appreciate if You have a solution for this.
Thank You in advance!
First you need to create an 'assets' folder under the 'main' folder and put a font file like .ttf file in it.
assets folder path
this is casual font download link
https://befonts.com/casual-font.html
I hope this is the answer you want. thank you
example)
Typeface mTfRegular = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Regular.ttf");
setTypeface(mTfRegular)
Another easy way to do this is to use the font family.
this way,
Create a folder called font in the /app/src/main/res/ path, put the font file inside, and write the contents of the Font Family in xml in /app/src/main/res/xml, and then apply the TextView or EditText you want to apply. Apply it using the android: fontFamily property.
Writing the Font Family in XML,
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/casual_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/casual_italic" />
</font-family>
Apply Fonts to FontView Using TextView,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/casual"/>
This helped me to solve it:
1 - Creating a font directory in resources (res) folder.
2 - Drag & Drop .tff file in the font directory (example.tff).
And do following:
Typeface myFont = ResourcesCompat.getFont(this, R.font.times);
Source (see accepted answer): Link

Android special Unicode characters

I am currently developing an app which displays special unicode characters (e.g. ꁴ)
Now I encountered that on older devices those symbols can not be displayed.
How can I know if it works on the current device or not?
Do I have to create an Virtual Android Device for each SDK-Version and test?
Would it be possible to bundle the current Roboto Font into my apk and deliver it to older devices so that the characters will be displayed?
if place your custom font in assets folder and address when used you will not have an issue
place your Roboto.ttf in assests folder
Created an entry in strings.xml for each icon. Eg for a "Yi Syllable Bby":
<string name="icon_note">ꁴ</string>
Referenced said entry in the view of my xml layout:
<Button
android:id="#+id/like"
style="?android:attr/buttonStyleSmall"
...
android:text="#string/icon_note" />
Loaded the font in my onCreate method and set it for the appropriate Views:
Typeface font = Typeface.createFromAsset( getAssets(), "Roboto.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
it can be used to use icon lib as font check here How to use icons and symbols from "Font Awesome" on Native Android Application

The new Custom Font Method in android using xml

As android released its O preview, a new feature has been added called fonts in xml. Its easy to implement but i have few doubts.
let me first add the best way to do it
1.)Right-click the res folder and go to New > Android resource directory. The New
Resource Directory window appears.
2.)In the Resource type list, select font, and then click OK.
3.)Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
4.)Double-click a font file to preview the file's fonts in the editor.
Next we must create a font family
1.)Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
2.)Enter the file name, and then click OK. The new font resource XML opens in the editor.
3.)Enclose each font file, style, and weight attribute in the font tag element. The following XML illustrates adding font-related attributes in the font resource XML:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="#font/hey_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/hey_bababa" />
</font-family>
Adding fonts to a TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="#font/ba_ba"**/>
As from the documentation all the steps are correct.my question is:
1.)Can this work in android lollipop or marshmallow
2.)Is any support library needed for implementing the features
3.)What type of font extension will this support like .ttf .otf
Please give me the best explanation.
1.)Can this work in android lollipop or marshmallow
Ans : It's work for lower Version also(Based on my research).
2.)Is any support library needed for implementing the features
Ans : No need support Lib only you need to update your sdk(Android O)
3.)What type of font extension will this support like .ttf .otf
Ans: Yes, Its support .ttf, .otf font Files.
For more info you can see below links :
Android Doc for Font Family Api , Android-O Preview Video

How to get Hindi fonts in Android..?

I'm new to Android field . I have followed the procedure of getting Hindi fonts on emulator but still Hindi characters are appearing in square boxes. Please help me how to proceed. I m using android 2.2 ,juno version. Any help regarding above problem will be highly appreciated.
Set The Hindi Font on TextView in Android
TextView t = new TextView(this);
Typeface Hindi = Typeface.createFromAsset(getAssets(), "fonts/mangle.ttf");
t.setTypeface(Hindi);
t.setText("Naveen Tamrakar");
Android does not yet fully support Hindi (droid fonts):
Read this link.
Create a folder in assets as "fonts".
Place your .ttf file of font in that.
then try the code as :
Typeface fontHindi = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
tv.setTypeface(fontHindi);
It works for me. Hope it works for you too.
You can download hindi fonts from http://hindi-fonts.com and copy it in /system/font directory of your phone. The font will be installed.
First of all download hindi font, d the OTF/TTF file you get, save that in asset folder, eclipse has already the folder, if you are using android studio, then make a new folder in project/app/src/main.
Then in java file, suppose you have to apply your font on TextView t1, use the code..
Typeface mytypeface= Typeface.createFromAsset(getAssets(),"font-file_name.TTF");
t1.setTypeface(mytypeface);
This way you can use Hindi font in Android.
Step 1: Add All Hindi text in res --> values --> string.xml file
<resources>
<string name="app_name">गन्ने से समृद्धि</string>
<string name="ganne_ki_kheti">गन्ने की खेती</string>
<string name="sampark">संपर्क</string>
Step 2: In layout xml file add text tag as android:text="#string/your_text".
For Example see below.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:text="#string/ganne_ki_kheti"
android:textSize="32sp"
android:textStyle="bold" />

Android Arabic/Farsi Fonts

I am developing an Android aap that uses Arabic Fonts.
I have successfully display Arabic text by using "tahoma.ttf"
But problem is that these Arabic Fonts Didn't give a cool look.
So i wanted to change that Font Style. Is there any way of doing this ?
If you want to use external font do :
First step is to pick a font that you want to use.
Create a fonts folder in your assets directory and copy your font there.
To access your custom font,use the Typeface class in the Android SDK to create a typeface that Android can use, then set any display elements that need to use your custom font appropriately.
Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset.You can do like this in your code:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
Note: Android does not seem to like all TrueType fonts.So, if you try to use a different font and it does not seem to be working, it may be that the font is incompatible with Android, for whatever reason.
You can see more details in these pages:
Fun with Fonts
Musings of the Bare Bones Coder/using custom fonts

Categories

Resources