Skip to content
Related Articles

Related Articles

Subscript and Superscript a String in Android with Kotlin

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 06 Feb, 2022

In science, a scientific expression defining a context is a combination of a finite number of symbols according to the rules of the context. So in a basic sense, such expressions are formed by various writing styles or scripts such as lower case, upper case, symbols, numbers, subscripts, superscripts, etc.

 Apply Subscript and Superscript to Text

So in this article, we will show you how you could apply subscript or superscript to text in TextView in Android. Follow the below steps once the IDE is ready.

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 to display the expression.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />

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.


package org.geeksforgeeks.subscriptsuperscript
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing 
        // the TextView from the layout file
        val mTextView = findViewById<TextView>(
        // Declaring a string
        val mString = "A2 + B6"
        // Creating a string span
        val mStringSpan = SpannableStringBuilder(mString)
        // Subscripting the string span for "2"
        mStringSpan.setSpan(SubscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        // Setting the text size ratio for "2" 
        // with respect to rest of the span
        mStringSpan.setSpan(RelativeSizeSpan(0.5f),1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        // Superscripting the string span for "6"
        mStringSpan.setSpan(SuperscriptSpan(), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        // Setting the text size ratio for "6" with
        // respect to rest of the span
        mStringSpan.setSpan(RelativeSizeSpan(0.5f),6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        // Setting the string
        // span to TextView
        mTextView.text = mStringSpan


You can see that the string is displayed as desired.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!