Subscript and Superscript a String in Android with Kotlin
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.
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)
val mTextView = findViewById<TextView>(R.id.text_view_1)
val mString = "A2 + B6"
val mStringSpan = SpannableStringBuilder(mString)
mStringSpan.setSpan(SubscriptSpan(), 1 , 2 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
mStringSpan.setSpan(RelativeSizeSpan( 0 .5f), 1 , 2 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
mStringSpan.setSpan(SuperscriptSpan(), 6 , 7 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
mStringSpan.setSpan(RelativeSizeSpan( 0 .5f), 6 , 7 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
mTextView.text = mStringSpan
}
}
|
Output:
You can see that the string is displayed as desired.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...