Different types of font resources in Xamarin and how to use them
9 minute(s) read
|
Published on: Apr 11, 2021
Updated on: Dec 14, 2021
|
In order for the operating system to support these fonts and show exactly what is in the application, it is necessary to apply special settings to support the fonts in the application. In this tutorial, we will teach you how to apply settings to the operating system to support application fonts. If you want to know about them, follow this tutorial.
The ways to use fonts in operating systems and applications are as follows:
1- One way to use fonts and display them is to package all fonts as the source of Android 2- and put them in the application and operating system. In this case, it is clear that all fonts are always present in the Android operating system, and when needed, you can refer to its source. Packing fonts and placing them on the Android source allows us to access the fonts at all times.
3- The second way to use fonts is to download fonts. In this case, the Android operating system also supports the download of fonts, so that if necessary, we download the relevant font and save it inside the Android operating system. Fonts are downloaded by the font provider checks to see if the corresponding it is present within the operating system. If there is no relevant it, the it provider will download it and save it inside the Android operating system.
It should be noted that all fonts that are similar or similar and have the same style are classified in one group. Fonts that have the same style and are similar to each other are grouped into groups such as families. Users must consider unique specifications in order to use these similar fonts. In this way, they can choose their unique names or specify their characteristics. Android Support Library v26 uses API level 26 to support and use fonts. If we consider APIs whose level is less than 26, then we have to specify android: namespace and app: namespace for them. If we do not specify android: namespace and app : namespace for them and consider only android: namespace, they may not be displayed on lower than 26 operating systems. So in order for all fonts to be displayed with an API level less than 26, both android: namespace and app: namespace must be specified, otherwise if not specified, both fonts will not be displayed. APIs with a level below 26 do not support fonts and must be set to android: namespace and app: namespace in order to be displayed.
The code snippets below are used to display the family in API level 14 and above.
To display different types of fonts in TextView, you need to make the appropriate settings. For example, the following code snippet describes how to display different types of fonts in TextView.
Source of FontPackage in Android
If the this are in the form of Packages, it means that the fonts are guaranteed to be inside the operating system and can always be used. Font files in TTF or TOF format like other programs are added to Xamarin. Android and stored in the operating system. The files are located in the Resources folder of the Xamarin.Android project.
Note:
its must have Built Action from AndroidResource.
Fonts in a family and group
Fonts that have the same style and are similar to each other are placed in a group called Families. The family is saved with the elements in the XML file. The XML file is also stored in the Resources / path.
Each family must have its own XML file to be saved with its and elements. If you want to create a fontfamily, you must add all the fonts to the Resources / folder. Then create an XML file inside the created this folder.
The XML file creates a font family below and stores them in the Resources / font folder named sourcesanspro.xml.
Three different values can be given for the fontStyle attribute:
- normal
- Italic
- bold
The fontWeight feature is used to specify the font thickness and can take different values:
- Thin - 100
- Extra Light - 200
- Light - 300
- Normal - 400
- Medium - 500
- Semi Bold - 600
- Bold - 700
- Extra Bold - 800
- Black - 900
The code snippet below defines a family of with Italic text style features and a thickness of 400:
Provide custom font settings with programming
The code below shows how programming can be used to display fonts inside TextView:
If you use the GetFont method, it automatically selects the first in the family inside the folder, but if you want to choose a font with special features, you can use the Typeface.Create method. This method provides you with the fonts you want.
Selects the code below the font whose Typeface is bold for display in TextView.
Download fonts
Instead of using packaged and storing them inside the operating system, you can download the fonts and temporarily save the audio inside the operating system or application. In this case, you also reduce the size of the APK. If you use packaged fonts, the size of the APK will increase, but if you download the fonts from various sources and temporarily store them in applications and operating systems, their size will decrease.
this are downloaded from various sources by the provider. Android operating system with version 8.0 has a provider for downloading from various sources that download fonts from this version from Google Font Repository.
How the font provider works is that after requesting a program or application for a font with special features and specifications, the font provider checks whether there are fonts with the same features and specifications in the system. If there is no font with the same features and specifications, the font provider tries to download the font from different sources and submit it to the program and application. After downloading the font from a source, in addition to the program and application request Other programs and applications can also use the downloaded font, although it should be noted that applications do not request the font directly from the font provider, but applications and applications will use the FontsContract API.
How to support Android operating system with version 8.0 of downloading from various sources:
1- Announcing downloadable for the Android operating system as a source: In this case, the program announces the downloadable it as XML resource files. Android saves these fonts on the system after downloading and integrating them.
Programming:
A FontRequest is considered for each and FontRequest is sent to the FontsContract class. FontsContract actually takes FontRequest and then receives the desired from the provider.
2- resources files must be added to the Xamarin.Android project and application before the fonts are downloaded. First, the are placed inside the XML file in the Resources / font folder.
The code below shows how to download the fonts in full:
The information that Android needs to receive and download is as follows:
fontProviderAuthority:
Allows the Font Provider to check and use the request.
fontQuery:
is actually a string that helps the it provider to find the desired font.
fontProviderCerts:
is a resource presentation.
FontCertificates
If the it provider is not already installed on the device, the Android operating system will need some this provider security certificates. These certificates are stored inside the resource array file in the Resources / values directory and path.
For example, the code below is an XML file that stores Google provider certificates.
After these codes, the following code must be added to them:
Download using the API for use in applications installed on the Android operating system
If you use the FontContractCompat.RequestFont method to download, this method will first check if the requested font is present within the system itself. If the desired is not inside the system, the provider will start searching for that font from various sources and finally download it and use it inside the application and system. FontRequest is the same request that is sent to the provider and requests the download of the with the desired specifications.
If the desired does not exist inside the system and the provider cannot download it, then the default font of the Android operating system will be used.
FontRequest has four sections:
this Provider Authority: This part of FontRequest allows the Provider to use the request and the Provider downloads the desired and places it in the system according to the request.
Package:
this Provider is used to authenticate and actually allows it to use the request.
fontQuery:
This section is a string that contains details and information about.
fontProviderCerts:
This section is also a resource presentation.
The following code describes FontRequest simulation:
The code below also indirectly downloads the this from the Google Fonts Open Source collection.
In this tutorial, you will get acquainted with the APIs used in version 8.0 of Android. How to place fonts inside the APK and use and display them in the layout in this tutorial, was taught. You can apply special settings using the codes included in this tutorial and you can use different by using two methods of programming and downloading.
DotNek Android development services