Android: add custom fonts to system - android

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);

Related

How do you use a customized font with a single file for android in react native?

I added a customized font to my react native project. The font only has a single ttf file (e.g. FontFamilyName.ttf), instead of multiple files for each style (e.g. FontFamilyName-Regular.ttf, FontFamilyName-Bold.ttf, FontFamilyName-SemiBold.ttf, FontFamilyName-Thin.ttf). But it is a font collection that contains multiple styles like Bold, Thin, SemiBold, etc. I can see all the variants with the Mac App FontBook.
For iOS, I can successfully use every style of the font by specifying "fontFamily" with the postscript name of each style (e.g. FontFamilyName-Thin).
But for Android, I can only use the default style - "regular" by specifying "fontFamily" with the name of the ttf file (i.e. FontFamilyName). I can't find a way to use other styles. Specifying "fontFamily" with the postscript name of a style like I do for iOS doesn't work for Android.
Create a new directory called "fonts" in the "assets" directory of your React Native project.
Copy the custom font file (e.g. TTF or OTF file) into the "fonts" directory.
In your React Native app's "android/app/build.gradle" file, add the following line to the "android" section:
assets.srcDirs = ['assets/fonts']
In your React Native app's "android/app/src/main/res/values/styles.xml" file, add the following line to the "resources" section:
style name="CustomFont" parent="android:TextAppearance.Material.Body1">
item name="android:fontFamily">#font/YOUR_CUSTOM_FONT_NAME /item> /style>
In your JS code, you can apply the custom font to a specific text by adding the fontFamily style, like so:
<Text style={{ fontFamily: 'CustomFont' }}>Hello World
Lastly, rebuild your app using "react-native run-android" command.
Note: If you have multiple custom fonts, you will need to repeat step 4 for each font, using a different style name each time.
ps: for point 4. style code need to remove < before style tag and item tag as stackoverflow was discarding the text in the comments
For those who have the same problem using variable fonts, which are single-filed, currently the best solution I've found is to use an app called 'Slice' to slice the font variations from the single file and add the separate font files to your project.
For how to use Slice, check out https://medium.com/timeless/adding-custom-variable-fonts-in-react-native-47e0d062bcfc
For how to add custom fonts to your project, check out https://mehrankhandev.medium.com/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4

Using Material Design Icons with Xamarin Forms - What Am I Missing?

I am trying to modify the default Xamarin Forms (Flyout) App template to use Material Design Icons for the FlyoutItem icons, instead of the supplied .png files. I have tried to follow this blog post by James Montemagno, and tried to use his Hanselman.Forms project as a reference... but I'm missing something.
Note: At this point, I don't have access to an iPhone or a Mac, so I'm just concentrating on the Android project.
I have done the following steps:
Imported the materialdesignicons-webfont.ttf file into the Assets folder and double-checked that its Build Action is set to AndroidAsset.
Added the following to the App.xaml file:
<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>
<x:String x:Key="IconAbout"></x:String>
Modified the AppShell.xaml to change the icon:
<FlyoutItem Title="About">
<FlyoutItem.Icon>
<FontImageSource Glyph="{StaticResource IconAbout}"
FontFamily="{StaticResource MaterialFontFamily}"
Color="Black"/>
</FlyoutItem.Icon>
<ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
</FlyoutItem>
This is a direct copy from the Hanselman.Forms project's TabItem - I thought the FontFamily would have to be a DynamicResource, but apparently not, because it works as is in that project, but doesn't work in mine either way - the icon is always blank (actually, if I change the Glyph to a letter A I get an "A" for an icon, but that's not really helpful).
I'm missing some dumb little detail that I just can't see.
I've gotten it to work.
First of all, go to this GitHub repo and download this material font family as a .tff file. Click on the 'font' file in the repo and download MaterialIcons-Regular.ttf.
Link: https://github.com/google/material-design-icons
Place the .tff file in your shared project.
Now reference the font in the assemblyinfo.cs file like so:
[assembly: ExportFont("MaterialIcons-Regular.ttf", Alias = "Material")]
Set the build action as 'embedded resource' on the .tff file. Do not forget this step!
Go this page https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.codepoints - to view all of the material codepoints.
In this case I will use the 'alarm-add' icon as an example (because why not). If you want to use the 'alarm-add' icon - find the 'alarm-add' codepoint. In this case the code for the 'alarm-add' icon's codepoint is e856.
Paste the following code into your shell:
<ShellContent Title="About"
ContentTemplate="{DataTemplate local:AboutPage}"
>
<ShellContent.Icon>
<FontImageSource FontFamily="Material"
Color="White"
Glyph="">
</FontImageSource>
</ShellContent.Icon>
</ShellContent>
If you follow all of the steps - the result should be like so:
If - by any chance - you want to use material icons outside of your shell you can create a Label which has the font family set as Material and as the Text property you can set the appropriate codepoint. Remember to include &#x before the codepoint - and always end it with a semicolon.
You can customise the colours and icon to whatever you like - just search the codepoint document for the icon you want and paste the codepoint which identifies that particular icon.
The source of the error is here:
<x:String x:Key="IconAbout"></x:String>
Changing that character code to 󰋟 got everything working - one missing zero and the result is chaos. Many thanks to #shaw and #tommy99 - trying to implement their answer (and it still not working) led me to the solution. I've upvoted their solution/comments in thanks.

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

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 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>

Categories

Resources