Custom fonts in android
Posted By : Rahul Baboria | 09-Mar-2021
Android Oreo introduced a lot of exciting and new features. On the off chance that you haven't as of now, you should check the engineers' site for the rundown of what's new in Android O. One of the extremely fascinating highlights for designers is the better approach to apply text styles in that spot in your XML records. But that it works out of the container for just API 26 (Android O). In this post, we will take a gander at how to backport this magnificent element to more seasoned renditions - down to API 14 - utilizing the Support Library 26. Already on Android, there were constrained approaches to utilize custom text styles on Android. The accompanying methods are the ones I think about the most well known methods for actualizing custom textual styles in Android:
1. Custom View:
One would commonly require a custom view that broadens the equal view where attempting to apply a textual style to. In the custom view, one would make an and after that call setTypeface (or a comparable technique, that, sets the typeface). One would likewise need the textual style record set in the benefits envelope.
The code in the custom view regularly resembles:
Because of some wonderful designers, there is another way to deal with having custom textual styles in your applications - and the library is called Calligraphy. The use is genuinely direct, and the traps are plainly distinguished. Be that as it may, likewise with each library, it accompanies the punishment of including additional conditions in this manner expanding technique check.
On account of the great society at Google and their work on Android Support Library 26 it is presently conceivable to proclaim textual styles in XML - something many refer to as text style families, and furthermore automatically without the need of an additional library other than the help library (which you in all likelihood as of now use in your application at any rate).
Procedure:
1. Add the Font ttf files to the Fonts Resource Directory
Much the same as string and drawable assets, the text styles are currently accessible as assets. Initial phase in getting this going is to add the text style records to the res/textual style organizer in your undertaking. This guarantees they are bundled as assets, and are effectively open comparably we get to different kinds of resources:@font/lato in XML or as R.font.lato in code.
2. Create a Font Family
Textual style family is something that was presented in Android, and it is utilized to characterize a gathering of textual styles and their comparing style. So the framework can figure out what textual style asset to use for standard, intense and italic styles and weight designs. A commonplace textual style family resembles this:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- regular -->
<font
android:font="@font/lato_light"
android:fontStyle="normal"
android:fontWeight="200"
app:font="@font/lato_light"
app:fontStyle="normal"
app:fontWeight="200" />
<!-- italic -->
<font
android:font="@font/lato_light_italic"
android:fontStyle="italic"
android:fontWeight="200"
app:font="@font/lato_light_italic"
app:fontStyle="italic"
app:fontWeight="200" />
<!-- bold -->
<font
android:font="@font/lato_bold"
android:fontStyle="normal"
android:fontWeight="600"
app:font="@font/lato_bold"
app:fontStyle="normal"
app:fontWeight="600" />
</font-family>
An extremely imperative thing to note is that we needed to characterize characteristics utilizing both android and application namespaces. The application namespace is the thing that guarantees that the component is in reverse good.
iii. Utilizing the Fonts
Since we have characterized the text styles, we could either utilize it automatically, straightforwardly in XML designs, or through a style that will be connected to the view in the format.
We should view how we could utilize the custom text styles for each situation.
Utilizing the Font Programmatically :
We can choose to utilize this text style automatically in a way very like how we have dependably been doing, with the exception of that we're not making this from a "benefit". Or maybe, we'll be getting it as an asset. Doing this automatically (for in reverse similarity) will resemble:
Typeface typeface = ResourcesCompat.getFont(this, R.font.lato_light);
fontText.setTypeface(typeface);
In layout File:
<TextView
android:id="@+id/compare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:text="@string/my_string"
android:textColor="@color/colorBlue"
android:textSize="@dimen/text_size_15"
android:fontFamily="@font/lato__bold"
android:padding="@dimen/margin_10"/>
Utilizing the Font-Family through a Style
Another use of the textual style family is by means of a style (or content appearance). We could determine the fontFamily ascribe to utilize the textual style family made in step ii, and afterward thus utilize that as a content appearance or even style in the format. Characterizing this in the styles.xml document will look something like:
<style name="TextAppearance">
<item name="fontFamily">@font/app_font</item>
</style>
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Baboria
Rahul Baboria is having good knowledge over Android Application.