यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android TextView में लाइन ब्रेक (नई लाइन) कैसे जोड़े जाते हैं। \n एस्केप सीक्वेंस, HTML <br>, या string.xml में फॉर्मेटिंग का उपयोग किया जा सकता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में TextView जोड़ें
<?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"
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:text="@string/Questions"
android:textSize="24sp" />
</RelativeLayout>
चरण 3: MainActivity.kt में टेक्स्ट कलर सेट करें
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textView)
textView.setTextColor(Color.RED)
}
}
चरण 4: strings.xml में लाइन ब्रेक के साथ स्ट्रिंग परिभाषित करें
<resources>
<string name="app_name">Q5</string>
<string name="Questions">
1. What is your Favourite food?
2. Which is your favourite sport?
3. Whats your bucket list?
</string>
</resources>
नोट: XML में वास्तविक नई लाइनें (एंटर की) काम करती हैं। \n या <br> की आवश्यकता नहीं है।
चरण 5: कोड में डायनामिक लाइन ब्रेक
// तरीका 1: \n एस्केप सीक्वेंस
textView.text = "Line 1\nLine 2\nLine 3"
// तरीका 2: HTML fromHtml (डिप्रिकेटेड नहीं)
textView.text = Html.fromHtml("Line 1<br>Line 2<br>Line 3", Html.FROM_HTML_MODE_LEGACY)
// तरीका 3: बिल्डर पैटर्न
textView.text = buildString {
append("Line 1")
append('\n')
append("Line 2")
append('\n')
append("Line 3")
}
// तरीका 4: Kotlin मल्टी-लाइन स्ट्रिंग
textView.text = """
Line 1
Line 2
Line 3
""".trimIndent()
चरण 6: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.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 डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।