Custom Font to alphanumeric characters only Android.?
Categories:
Applying Custom Fonts to Alphanumeric Characters Only in Android

Learn how to selectively apply custom fonts to only alphanumeric characters within a TextView in Android, leaving symbols and other characters untouched.
Applying custom fonts in Android is a common practice to enhance the visual appeal of an application. However, there are scenarios where you might want to apply a custom font only to specific character sets, such as alphanumeric characters, while retaining the default system font for symbols, punctuation, or other special characters. This article will guide you through the process of achieving this selective font application in your Android TextViews.
The Challenge of Selective Font Application
By default, when you set a custom Typeface
on a TextView
, it applies to all characters within that TextView
's text. This can sometimes lead to undesirable visual inconsistencies if your custom font doesn't render symbols or special characters as gracefully as the system's default font. The goal is to create a SpannableString
that allows us to apply TypefaceSpan
only to the alphanumeric portions of the text.
flowchart TD A[Input Text] --> B{Iterate Characters} B -- Is Alphanumeric? --> C{Apply Custom Font Span} B -- Is Not Alphanumeric? --> D{Retain Default Font} C --> E[Build SpannableString] D --> E E --> F[Set to TextView]
Process for selectively applying custom fonts
Implementing a Custom Font Alphanumeric Filter
To achieve selective font application, we'll iterate through the input string, identify alphanumeric characters, and apply a TypefaceSpan
to those specific ranges. This requires creating a helper method that takes the original text and the custom Typeface
as input and returns a SpannableString
.
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.TypefaceSpan
import android.widget.TextView
fun TextView.setAlphanumericCustomFont(text: String, customTypeface: Typeface) {
val spannableString = SpannableString(text)
for (i in text.indices) {
val char = text[i]
if (char.isLetterOrDigit()) {
spannableString.setSpan(
TypefaceSpan(customTypeface),
i,
i + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
this.text = spannableString
}
Kotlin extension function for applying custom font to alphanumeric characters
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TypefaceSpan;
import android.widget.TextView;
public class FontUtils {
public static void setAlphanumericCustomFont(TextView textView, String text, Typeface customTypeface) {
SpannableString spannableString = new SpannableString(text);
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetterOrDigit(c)) {
spannableString.setSpan(
new TypefaceSpan(customTypeface),
i,
i + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}
textView.setText(spannableString);
}
}
Java helper method for applying custom font to alphanumeric characters
isLetterOrDigit()
check or pre-calculating ranges if the text is static. However, for typical UI elements, the character-by-character iteration is usually sufficient.Usage Example
Once you have the helper function, using it is straightforward. First, load your custom font, then call the function on your TextView
with the desired text and Typeface
.
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myTextView: TextView = findViewById(R.id.myTextView)
val customFont = Typeface.createFromAsset(assets, "fonts/MyCustomFont.ttf")
val originalText = "Hello World! 123 @#$ This is a test."
myTextView.setAlphanumericCustomFont(originalText, customFont)
}
}
Example of using the Kotlin extension function in an Activity
import android.graphics.Typeface;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextView = findViewById(R.id.myTextView);
Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/MyCustomFont.ttf");
String originalText = "Hello World! 123 @#$ This is a test.";
FontUtils.setAlphanumericCustomFont(myTextView, originalText, customFont);
}
}
Example of using the Java helper method in an Activity
MyCustomFont.ttf
) is placed in the src/main/assets/fonts/
directory of your Android project. If the fonts
folder doesn't exist, create it.