How can we change the font in Android systems and how to solve of problems?
13 minute(s) read
|
Published on: May 11, 2021
Updated on: Dec 14, 2021
|
And use some features of Android systems so that we can finally change the fonts used in Android systems and applications (change font in android app ). Different types of fonts can be used in Android systems. This tutorial will learn how to change fonts and apply related settings. If you want to know how to change fonts in Android systems and applications, it is recommended that you follow this article.
Here is what you will learn in this tutorial:
- How to change fonts in Android apps - How to change Widget fonts in XML
- How to set fonts using Typeface
- Use the Calligraphy library to change all fonts in the program
- Review and answer common problems and questions in Android programming with their solutions
How to switch Widget fonts in XML?
To change the fonts and use different types of fonts available in Android systems, we must do the following.
switch font in an android studio project
1- Open a new project in Android Studio and select the desired name. The custom name selected in this tutorial is CustomFont. (change font in android studio)
2- The type of activity that we must choose in this part of the training for this project is Empty Activity.
3- It should be noted that font change settings can be applied without using Java language and methods, so there is no emphasis on choosing Java language in this part of training and project creation.
4- With the introduction of API 26 in the three versions of Android, a new feature has been added that can specify different types of fonts for the widget. Specifying fonts for the widget using this method eliminates the need to use Java methods.
5 - n this method of specifying fonts, you can add the fontFamily property for each widget and specify the fonts for them.
6- To do this, we must add the appcompat-v7 library to the project because the appcompat-v7 library is one of the most essential to apply these settings to change the font.
7- Add a directory with the desired font name to res.
8- Then, we have to save our desired font file using .ttf or .otf extensions and add it to this directory. It is better to be careful in choosing fonts and use fonts that are slang and positively affect user interface design.
Note:
It is better to use lower case letters, numbers, and lines to name and choose fonts. We can choose the names we want for the fonts, and we will use the names we have chosen for them in the definition in this project.
9- Use the fontFamily property and add the font to a TextView.
10- The codes that should be in the activity_main.xml section are as follows:
After entering the above code, it is possible to preview the fonts, and you can see the result and output of the font you have selected.
How to set fonts using Typeface
In this part of the tutorial, we want to send our desired font to the widget id using the Typeface class inside the activity.
11- Add a TextView with the txt_two ID to the page.
12- Then, we have to complete the activity as follows.
13- The codes that should be in the MainActivity.java section are as follows:
In the code above, we define TextView in an activity in the first part.
In the second line of code above, Typeface is created, which is used to call the Caslon font from the font directory using the getFont () method.
Note that the font folder is located inside the res directory. So, you can say that the getResources () method was written before getFont () was written.
Finally, in the code above, apply the font defined in tf to txtTwo using the setTypeface () method.
14- After executing the project, we will see a problem with the getFont method.
15- It should be noted that this method can be used in API versions 26 and above.
16- In other words, if we run the project we created in lower versions, the font will not change.
17- To solve this problem, we have to implement Typeface differently.
18- We use a method called createFromAsset. This method takes the desired font file from the assets folder.
19- In this project, we do not have the folder. To do this, we must first create it. Like the following:
20- Then, select the Finish option to add the folder in the window that opens.
21- Now, we need to add the fonts to this folder again.
22- Copy and paste the desired font from the previous folder called font.
After entering the above code, we see that the txt_three ID defines the third TextView.
Use the Calligraphy library to exchange all fonts in the app.
In the previous methods, the font was changed to a state that we had to define separately for each widget. These methods are suitable for situations where Android systems have fewer widgets or want to change the font settings for a limited number of Apply widgets. In this case, it is very convenient to use the methods listed above, but when we want to set many widgets in Android systems so that the fonts change, we must use another method. Another method used to change the font of a large number of widgets in Android systems is mentioned in this part of the tutorial.
The best way to change the font and with more DJs is to use the Calligraphy library. It should be noted that it was possible to create errors in the previous methods and waste a lot of time applying font changes in many widgets.
One of the people inside the GitHub site has this library, which we also use. You can visit the library created by clicking on its link. https://github.com/chrisjenx/Calligraphy 23- According to the description on the GitHub site, we must first place the library inside the dependencies block in build. Gradle (app).
24- Then, we have to sync the project to add the library to the project.
25- Then, we must add a class to the main project package.
26- The class created in the main package of the project must be inherited from the Application.
27- The name we chose for the class created in the tutorial is Calligraphy.
28- Add the onCreate () method to the class. Like the following:
29- Using the android: name property, we must introduce this class in the manifest section of the AndroidManifest.xml project.
30- Then, we have to put the following method inside the activities we want the fonts to be applied.
31- The code that should be in the MainActivity.java section is as follows:
32- We consider an alternative toolbar of the action bar to better understand and see the result.
33- The complete and final codes of the activity_main.xml section are as follows:
exchange font in android code
34- We are implementing the project.
35- After running the project, we will see the result and output of all our changes.
36- It should be noted that font changes can be applied on widgets and through XML using Calligraphy without using Java codes and methods.
37- In this method, instead of using the fontFamily property, we use the font path property.
38- Then, I add another footpath font to the fonts folder in assets.
39- Then, I add a button to the page and define the font using the font path. Like the following:
change font button android studio
After applying the code listed above, the second and new button we defined will be displayed in Circular font.
40- It should be noted that font properties can be defined in one style. With this method, we do not need to apply the settings for applying font changes to the widgets one by one.
Note:
It should be noted that the font path is without the prefix's android: and app. Do not pay attention to the warning displayed by Android Studio because it will not interfere with your project's performance and interfere with its results. But if you want to remove that warning, you can go to the GitHub site.
GitHub link:
https://github.com/
Note:
It should be noted that you must be careful in choosing fonts to choose good and effective fonts to apply changes.
Review and answer common problems and questions
In this part of the tutorial, all the problems, problems, and errors that application designers and developers face when programming Android are mentioned. If you want to get acquainted with the questions, common problems, and their solutions, it is recommended to pay attention to continuing this training.
Disable Gradle's' offline mode' and sync project: If you see this error while programming and developing the application, it means that Gradle is offline to receive the required information and data and libraries and tools. Add to the project requires an Internet connection.
- Gradle sync failed:
Failed to open zip file: If you see this error, it is better to close the input environment of the studio and open it again. Go to C: \ Users \ user_name \. Gradle \ wrapper \ dists and delete all its contents. After reopening the Android Studio environment, you must allow Gradle to be downloaded and re-installed.
- Could not find… and Searched in the following locations…:
If you encounter this warning, check to see the dependencies blocks in the build. Gradle File defines google () and center () repositories or not. If one of these repositories is defined, you must define the next item and repository. repositories {
google ()
center ()
}
- Convert ConstraintLayout to RelativeLayout or LinearLayout:
It should be noted that the layout that is created to create a project in version 2.3 of Android is ConstraintLayout. To use this layer, replace android. Support. Constraint. ConstraintLayout with RelativeLayout.
- Error: CreateProcess error = 216, This version of% one is not compatible with the version of Windows you're running…:
If you encounter this error, you should check to see if you have installed the old version of Java or not. If you have an older version installed, you will need to replace the older version with a newer one. Then you have to go to File -> Project Structure in Android Studio and uncheck Use Embedded JDK and replace the path where JDK is installed.
- Error: Failed to find the target with hash string ‘android-xx' in [Your SDK Location]:
In this error, xx indicates the API version. For example, we can say that if we get this error using android-26, it means that we need platform26, and it is not installed. We need to update the Build tools and Platform tools to the latest version in the SDK Manager section to fix this error. Then go to File> Invalid Caches / Restart and delete the cache. Finally, we have to clean the project once and rebuild it again.
- Unable to unpack file android-sdk_r22.6.2-windows.zip:
Error in opening zip file. Make sure you have enough disk space on the destination drive and retry: If you see this warning, it means that there is not enough space to install.
- Failed to resolve: JUnit: JUnit: 4.12 and prolong the project build:
To use the JUnit library, removing the code below from the build is better. Gradle section and sync the project again and run it.
test Compile 'junit: junit: 4.12'
- Genymotion (or other emulators), although running, is not displayed in the Connected Devices list:
We will have this problem due to port interference, which warns us with the following message:
SDK \ platform-tools \ adb.exe start-server 'failed - run manually if necessary
To solve this problem, open Task Manager, end adb.exe. Before launching the device, press the Run button of Android Studio to give you the message of not identifying the device. Then close the corresponding window and launch the device.
- Reddening of Rafter project construction:
If you encounter this problem, the project is not fully built.
You have to click the Make button to rebuild the project to solve this problem. Or you can rebuild the project to solve it.
- Preview page not displayed:
To solve this problem, you must change the API version in the Preview section. Of course, it should be noted that you can only use APIs that are installed in the SDK.
What is explained in this tutorial is as follows:
- How to change fonts in Android apps - How to change Widget fonts in XML
- How to set fonts using Typeface
- Use the Calligraphy library to change all fonts in the program
- Review and answer common problems and questions in Android programming with their solutions
About our Android and iOS app development services