परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में SpannableStringBuilder और SubscriptSpan/SuperscriptSpan का उपयोग करके टेक्स्ट को सबस्क्रिप्ट और सुपरस्क्रिप्ट कैसे बनाया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML कॉन्फ़िगर करें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:fontFamily="sans-serif-condensed"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="36sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import android.text.style.SubscriptSpan
import android.text.style.SuperscriptSpan
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private lateinit var strText: String
private lateinit var spannableStringBuilder: SpannableStringBuilder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
strText = "टेस्ट टेक्स्ट सबस्क्रिप्ट X2 और सुपरस्क्रिप्ट Y5 उदाहरण दिखाने के लिए।"
spannableStringBuilder = SpannableStringBuilder(strText)
val subscriptSpan = SubscriptSpan()
val superscriptSpan = SuperscriptSpan()
// '2' को सबस्क्रिप्ट बनाएं
val index2 = strText.indexOf("2")
spannableStringBuilder.setSpan(
subscriptSpan, index2, index2 + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
// '5' को सुपरस्क्रिप्ट बनाएं
val index5 = strText.indexOf("5")
spannableStringBuilder.setSpan(
superscriptSpan, index5, index5 + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
// दोनों के लिए छोटा फॉन्ट साइज सेट करें
showSmallSizeText("2")
showSmallSizeText("5")
textView.text = spannableStringBuilder
}
private fun showSmallSizeText(string: String) {
val relativeSizeSpan = RelativeSizeSpan(0.5f)
val index = strText.indexOf(string)
spannableStringBuilder.setSpan(
relativeSizeSpan, index, index + string.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
चरण 4: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
स्पैन के प्रकार
- SubscriptSpan: टेक्स्ट को बेसलाइन से नीचे ले जाता है (जैसे H₂O में 2)
- SuperscriptSpan: टेक्स्ट को बेसलाइन से ऊपर ले जाता है (जैसे x² में ²)
- RelativeSizeSpan: टेक्स्ट का सापेक्ष आकार बदलता है (0.5f = आधा आकार)