Best practice when using Text in Android

Tram Ho

The most commonly used component in Android apps is Text. We use Text as TextView or as EditText. So for a better performance application, we have to use Text in the best way possible. In this article, we will learn some best practices for using Text in Android. Outline of the article:

  1. Improving Text performance
  2. Text Styling
  3. Custom Fonts
  4. System Fonts
  5. Editable Text
  6. Conclude

Improving Text performance

As we all know, in an Android application, nearly 70% of the application contains text and therefore, the performance of the application depends heavily on Text. There are many studies that have been done in recent years and in Google I / O 2019, the following changes were announced:

Hyphenation (hyphens): Currently, hyphens are disabled by default in Android Q and AppCompact v1.1.0. This is because hyphens have a great influence on the performance of Text. To be more precise, 70% of word processing jobs are for hyphens. To enable hyphens, you must add the following code in style.xml :

Or you can add your TextView with the following property:

Precomputed Text : This is a tool used to perform text processing in the background thread. As a result, it does 90% of the processing of TextView before the View is layout. This tool is supported from API 21 onwards. In I / O 2019, new APIs have been added for easy integration with RecyclerView’s prefetch mechanism. Let’s learn more about RecyclerView’s prefetch. In the image below, we have two threads, the UI thread and the Render stream. Eye icon shows the time of VSYNC. Under normal circumstances, there is no problem with VSYNC. But if some work takes time and cannot finish until the next VSYNC then it will block this next VSYNC. This can happen when you scroll down the page and the next image appears above the previous image. So we can do some work when the stream is in an ideal state. This is called prefetch when we do the work before we encounter the VSYNC overlap.

The problem here is that if this process still takes a very large amount of time, then VSYNC will be overlapping and will have UI errors (UI junk). One solution to this problem is that we can transfer some work to the background thread by using PrecomputingText and removing these junk UIs.

To move word processing jobs to a background stream, you can use the following methods:

Example of implementation:

Note: You should not change the text style after using getTextFuture but should do before that.

Text Styling

After improving the performance of Text, the next thing to be concerned about is Text Styling. There are many ways to style text. But which is better? Also, you should know the priority of the view.

Normally, we will style the text by using only the properties of TextView:

But the problem here is that by using the above method, we can only style one TextView. But what if you want to add the same font color to all TextViews? Therefore, to create uniformity in the style of the text, we perform the styling for the text in the styles.xml file:

In the above example, we have the textSize property in both TextView and in styles.xml file. The view’s properties will override the Style property, so here we will have a text with textSize of 16sp, not 12sp.

Every view has some default styles but they will be overridden by the properties we add manually. So the order of precedence will be:

View> Style> Default Style> TextAppearance

From Android Q, a new attribute added in TextAppparent is fontVariationSettings and we can use it in TextView and AppCompatTextView.

TextAppearanceSpan is updated to read and apply typography or shadows. In addition, two new spans have been added to Android Q, namely LineHeightSpan and LineBackgroundSpan. The properties set by Span will override any other properties View. So the new priority order will be:

Span> View> Style> Default Style> TextAppearance

If you want to make some changes at the application level, that is, if you want to change the font of the entire application, you can do this with the help of Themes:

And the last priority order is:

Span> View> Style> Default Style> Theme> TextAppearance

Custom Fonts

There are very few fonts available in Android and therefore, the concept of downloadable fonts and fonts is in XML.

Take a look at the image below:

Here, we have a button and a lock icon and 2 texts on the sides of the icon. As you can see we have a problem in the same button but we need 2 different font types for icons and text while we can only set 1 font type for 1 button. Here we can use typeface span to solve this problem.

However, this option is also not feasible if we support more than one language and with more than one font. Then the span does not solve the problem.

So in Android Q, CustomFallbackBuilder is added to the Typeface class. It allows you to create a typeface with many fonts. The following is an example of multilingual and multi-font:

Next is an example of icons and fonts that both have different fonts:

When creating a typeface with this builder, you can add up to 64 fonts.

In general, the system will perform a sequential search for fonts. For example, if the font is lato then it will search for lato . If lato is found, that font will be specified otherwise the system will use the fallback system font. The code that defines system font fallback:

So in the above example, if a character is not supported by lato, the system will use the sans-serif font for that character.

Font.Builder : While creating the font, you can set the weight and inclination of the font object.

System Fonts

Android supports over 100 languages ​​and all of these languages ​​may require different font files. For example, Hindi fonts require Devanagari fonts. Therefore, to support a large number of languages, Android devices have many font files. For example, with a pixel 3 machine, it has more than 270 font files installed. Therefore, these are called System Fonts.

To draw text, NDK applications such as a document viewer need to know what system fonts can display the given text. There are 2 APIs provided to support this work:

Font Matcher API : AFontMatcher_match is used to find which system fonts can display a given text. For example:

In the screenshot above, the first line of code will return the NotoSansCJK font and the length is 3. The second line will return the Roboto font and the length is 8 and the third line of code will return the NotoSansCJK font of a length of 2.

This API will never return a NullPointer . If no font is displayed, it will return a byte font object . And this font object will be used to draw missing symbols called Tofu . And if no fonts match, the API will return the font most similar to the requested font.

Font Enumeration API : If you want to know what font files are on your Android system, you can find these files with the help of Font Enumeration API. It will grant access to the font files installed by the system. See how to use:

Here, with the help of the getAvailableFonts () method, you can access all fonts available in the System.

In the case of NDK, we have to create a loop and then with the help of ASystemFontIterator, you can point to the next font and then use it.

The performance of Font Enumeration API is not so good. Therefore, you should store the results returned and reused instead of calling this method repeatedly.

Editable Text

One of the most used subclasses of TextView is EditText. In general, almost everything applied on TextView by default also applies to EditText. But beyond this, we should be interested in a few other things related to EditText. The process takes place while using EditText:

The first process is your application, the second is the soft keyboard used to get input from the device and the third is the system process that coordinates these two processes. All of this communication is called Inter-Process Communication. Therefore, delays in one process may result in the delay of other processes. EditText has the following six main components:

  1. Background : This is the background of the EditText, for example you can update the background color using this property.
  2. Cursor : That is the cursor that appears when you type something on EditText. You can also change the color of this cursor.
  3. Handle : This is the handle of the cursor. You can change the color of this handle.
  4. Edit Text Color : This is the color of the text included in EditText.
  5. Text Color Hint : This is the color of the suggested text in EditText.
  6. Text Highlight Color : It is the color of EditText that is displayed when you select some text on EditText.

There are many problems that can occur while using EditText. For example, while using EditText to enter a username, the following may occur:

Therefore, you must use EditText in the way that it will display error messages correctly and the message should not be hidden by the keyboard.

There is a very simple solution to the above problem. All you need to do is tell the system to use the yellow part instead of the blue part as shown below.

To do so, you must inherit from EditText and override 3 methods:

getFocusedRect () will find the next area in focus, getGlobalVisibleRect () identifies the display area of ​​a view and requestRectangleOnScreen () is used by EditText to ask the System to display a certain area on the screen.


In this article, we have learned some best practices for using text in Android. Because in an Android application, text makes up 70%, so it becomes necessary to use text in the most efficient way.


Share the news now

Source : Viblo