The new Custom Font Method in android using xml - android

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

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

Custom font not showing on device

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

Android: Russian Ruble currency not displayed on certain phone?

I am getting some JSON data with a formatted string of currency like this
₽35
However, i noticed that on a Nexus 5 (Lollipop) it displays it correctly but other phones such as the HTC one mini and Samsung GT-I9505, it displays a blank character.
I attempted to research the issue, i could not find a solution other than, in the XML layout file, ensure that this line is present
<?xml version="1.0" encoding="utf-8"?>
But i still have the same issue
Please help
Edit 15 May 2015
Loading custom font NotoSans (Please note I know this would leak memory but its just a quick test)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ruble = (TextView)findViewById(R.id.ruble);
Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/NotoSans-Regular.ttf");
ruble.setTypeface(myFont);
}
The Russian Ruble symbol defined in strings.xml (note I tried all)
<string name="rubleSymbolJava">\u20BD</string>
<string name="rubleSymbolHTML">₽</string>
<string name="rubleSymbolHTMLHex">₽</string>
Same problem, on older phones shown as a square but works on Android 5.0 BUT nothing older.
SOLVED Please see my answer
I fixed this. I just used the new Google Roboto Font which was last updated Jan 2015 and has the Ruble symbol.
https://www.google.com/fonts/specimen/Roboto#pairings
Research showed that in 2014 Russia changed their currency symbol so old fonts did not have the new Glyph so for others who have a similar issue, see if your font has been updated
The problem-phone's default font might be missing the Russian ruble glyph. Try to hard-code it into a TextView somewhere in your app to make sure that that is indeed the issue rather than some other JSON parsing mistake or so (seems less likely as it works on some phones).
In case the missing glyph is your problem, it's fairly simple to include your own font in your project. That way you can be sure that you can support all glyphs that you need.
Here are the steps to do so programmatically:
Place the .ttf file of your font into the folder assets/fonts in your project
Load it as a TypeFace object via TypeFace myFont = Typeface.createFromAsset(activity.getAssets(), "fonts/font.ttf") (replace myFont and font.ttf as needed)
Assign it to all your TextViews, Paints, etc. via myTextView.setTypeface(myFont). There also seem to be some tricks that allow you to set it as the default font for your entire app.
I haven't used the graphical designer much, but I'm sure it's not any more difficult there either. Also, these instructions are for the Android Eclipse plugin. While the code will likely be identical in Android Studio, I'm not sure that the folder structure is the same. The docs say something about placing the assets in main/assets instead of just assets, for example...
A couple of things to consider:
Make sure that your distribution of the font you choose doesn't violate any licenses of that font.
Your app will now no longer use the device's default font, which will mean that your app will look more similar and predictable across devices, but it might stand out from other apps on specific devices that might use very different default fonts.
The size of the .ttf file will add to the size of your app. Most fonts are fairly small (~100KB), but some do get large (several MB), especially if they support many different languages, which might then become an issue.
Make sure your font supports all glyphs in all languages that your project needs.
An added bonus: You can add some of your own custom glyphs into the font to easily include simple single-colored vector graphics into your app just by putting the corresponding "letter" into a TextView (remember Wingdings?).
I found solution in that answer https://stackoverflow.com/a/36338231/1078641 , added &subset=all parameter to Roboto font link.
The original link to Roboto font was like that
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300italic,300,400italic,500,500italic,700,700italic' rel='stylesheet' type='text/css'>
After I added &subset=all parameter to the link I can successfully see ruble symbol on my Android device
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300italic,300,400italic,500,500italic,700,700italic&subset=all' rel='stylesheet' type='text/css'>
You can also check Android version. I think, some phones on API 19 support russian rubles, but on API 21 they all support it. It would be easier to add string resources instead of check and replace programmatically.
After reading Trying to use <!ENTITY in ANDROID resources with error: "The entity was referenced, but not declared." I made a new file strings.xml in res/values-v21.
So, in values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY rouble "р.">
]>
<resources>
<string name="saaki">Галстук Сааки - %1$s &rouble;</string>
</resources>
In values-v21/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY rouble "₽">
]>
<resources>
<string name="saaki">Галстук Сааки - %1$s &rouble;</string>
</resources>
You can check if the glyph is present using the code from
https://stackoverflow.com/a/29938454/755804
If it's not present, you will need to either provide an image and use TextViewWithImages or replace it with a character sequence.

How to Display Unicode font in eclipse layout designer for android

I have the following resource in my strings.xml
<string name="punjabi">ਪੰਜਾਬੀ</string>
This gets displayed in the Android string resource editor and my application just fine.
However in the eclipse layout editor, the characters display as boxes, similar to □□□□□□
The box characters indicate a font glyph look up failure.
Any ideas on how to get Eclipse to display unicode in the layout designer?
There are two things you need. The font itself, and the fallback mapping.
First, copy the font needed, such as Lohit-Punjabi.ttf into the platform specific fonts directory:
sdk\platforms\android-###\data\fonts
Second, add the fallback font into the font definition file in that directory:
For android SDK version up to 13 add the following line in fonts.xml
<fallback ttf="Lohit-Punjabi" />
For android SDK version 14 and up add the following line in fallback_fonts.xml
<family>
<fileset>
<file>Lohit-Punjabi.ttf</file>
</fileset>
</family>

Android: add custom fonts to system

I know how to use custom font in an app, but what I want to do is adding custom fonts system-wide, just like adding a new font on Windows. If there's no official way, which module of android source code should I read? I have to change android source code and build it to support custom fonts.
Here are steps to add custom font into Android build-in system:
Copy custom font .ttf into frameworks/base/data/fonts
Modify frameworks/base/data/fonts/Android.mk
Add your custom font into list of 'font_src_files'
font_src_files :=
Roboto-Regular.ttf
....
AndroidClock_Solid.ttf
<custom_font>.ttf \
Modify frameworks/base/data/fonts/fonts.mk
Add your custom font into list of PRODUCT_PACKAGES
PRODUCT_PACKAGES :=
DroidSansFallback.ttf
...
AndroidClock_Solid.ttf
<custom_font>.ttf \
Rebuild
NOTE: Check if your custom font exists in out/target/product/generic/system/fonts or not. If yes, your custom font have already included in system. If no, recheck your modification.
I'm working on Android 5.1.1 Lollipop.
Nguyen's answer supported me, but only to some extent. So I have to edit fonts.xml and fallback_fonts.xml files (as commented by Chef Pharaoh).
First do the steps Nguyen explained.
Add following lines in fonts.xml towards the end of the <familyset> element.
<family>
<font weight="400" style="normal"><custom_font>.ttf</font>
</family>
Add following lines in fallback_fonts.xml towards the end of the <familyset> element.
<family>
<fileset>
<file><custom_font>.ttf</file>
</fileset>
</family>
(replace <custom_font> with your custom font name)
I found this link a bit helpful, and it is in Chinese.
FYR following note is about the file fallback_fonts.xml
NOTE: this file is the legacy format, for compatibility with apps. The new,
more flexible format is fonts.xml. Please keep the two in sync until the legacy
format can be fully removed.
Download Helvetica.ttf file ,copy this file into assets folder and use this code.
Typeface font = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
your_textview_id.setTypeface(font);

Categories

Resources