Change Style of a Particular Span of Characters Inside a String in Android
Last Updated :
27 Sep, 2021
TextView in Android is used to display text or sequence of characters, better known as strings. These strings can be hard-coded as well as declared during the runtime. The text, in general, is displayed in a raw format where the same text formatting is applied to every character present in the string. If we wish to apply styles, the formatting is applied to the entire string. There is no direct provision to apply different styles to different characters.
So, in this article, we will show you how you could apply styles to sub-strings or string spans programmatically in Android.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView in the layout file. This TextView will display our formatted text.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="20sp"/>
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring TextView from the layout file
val mTextView = findViewById<TextView>(R.id.text_view)
// Declaring a string
val mString = "GeeksforGeeks is a Computer Science portal for Geeks"
// Getting a spannable string from the previous string
val mSpannableString = SpannableString(mString)
// Declaring Style Spans of different types
val mBoldSpan = StyleSpan(Typeface.BOLD)
val mItalicSpan = StyleSpan(Typeface.ITALIC)
val mBoldItalicSpan = StyleSpan(Typeface.BOLD_ITALIC)
// Applying style spans to the spannable string
mSpannableString.setSpan(mBoldSpan, 0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
mSpannableString.setSpan(mItalicSpan, 19, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
mSpannableString.setSpan(mBoldItalicSpan, 47, 52, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
// Displaying the formatted
// spannable string in the TextView
mTextView.text = mSpannableString
}
}
Output:
You can see that the string has been formatted at different spans.
Similar Reads
Add Hyperlink at a Particular Text Span in Android using Jetpack Compose In Android, a TextView is used to display text inside the activity on the screen. Similarly, in Jetpack Compose, a Text element is used to display text on the activity screen. By default, Text cannot create a hyperlink around the text, So, we create a hyperlink using Annonated String and display it
3 min read
How to Change Input Type of EditText Programmatically in Android? EditText in Android UI or applications is an input field, that is used to give input text to the application. This input text can be of multiple types like text (Characters), number (Integers), etc. These fields can be used to take text input from the users to perform any desired operation. Let us s
3 min read
How to change the Text Color of a Substring in android using SpannableString class? In this article, we will learn about how to change the text color of a substring of a string. It is easy to change the color of the whole string but to change the color of a substring we have to use a special class SpannableString. But SpannableString class is not really helpful when it comes to cha
2 min read
Different Ways to Search All the Occurrences of a String in the Entire Project in Android Studio When we are building a complex application in Android Studio, then it is always preferable to have a basic idea about the different shortcut keys which are used in Android studio to make your tasks easier. So in this article, we will take a look at some shortcut keys which we can use to find usages
3 min read
How to Use putExtra() and getExtra() For String Data in Android? Many times in android applications we have to pass data from one activity to another for performing some operations. There are several different ways that are used to give data from one activity to another activity. In this article, we will specifically take a look at How to use putExtra() and getEx
5 min read