Subscript and Superscript a String in Android with Kotlin
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.
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
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/text_view_1" android:layout_width = "200sp" android:layout_height = "200sp" android:textSize = "40sp" android:gravity = "center" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
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
package org.geeksforgeeks.subscriptsuperscript import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.Spannable import android.text.SpannableStringBuilder import android.text.style.RelativeSizeSpan import android.text.style.SubscriptSpan import android.text.style.SuperscriptSpan import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing // the TextView from the layout file val mTextView = findViewById<TextView>(R.id.text_view_1) // 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 } } |
Output:
You can see that the string is displayed as desired.